Fun with Infinite Loops in Delphi and Java

Look at the Java and Delphi source codes below. Which one will compile, and which one will run in an infinite loop? (click on ‘more…’ to see the answer)

Java:

void loop() {
 while (true) {
  try {
   return;
  } finally {
   continue;
  }
 }
}

Delphi:

procedure Loop;
begin
 while True do
 try
  Exit;
 finally
  Continue;
 end;
end;

Answer: Only the Java code compiles, and runs in an infinite loop. The Delphi compiler will stop with the error message “Cannot BREAK, CONTINUE or EXIT out of a FINALLY clause”.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s