*
The moose likes Programming Diversions and the fly likes Be the Compiler Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "Be the Compiler" Watch "Be the Compiler" New topic
Author

Be the Compiler

Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

Hi,

The following code snippet would help java-novice. wear the compiler's hat and say the output for the code snippets

1.

2.

Try and tell in which cases the code in finally block will be executed


Ashwin Sridhar
SCJP | SCWCD | OCA
anirudh jagithyala
Ranch Hand

Joined: Dec 07, 2010
Posts: 41
the code in the first case executes the finally block


try{
//some code
return;
}finally{

}


Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

yes. only the first one executes finally
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3001
    
    9
Which might mean something if there were any code in the finally block. Since there isn't, it seems a poor example to discuss.

Also, "be the compiler" has nothing to do with this. The compiler has no idea what "System.exit()" does - it just knows it's a method that can be called with the appropriate bytecode. It's the JVM that determines, at runtime, that System.exit() results in shutting down without executing finally.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

Mike Simmons wrote:The compiler has no idea what "System.exit()" does - it just knows it's a method that can be called with the appropriate bytecode.


Which is why
will compile, but
will complain about unreachable code.
 
 
subject: Be the Compiler
 
Similar Threads
Can i use "finally" withouy try-catch?
returning a value from try and finally
Try an Finally Working Confusion
Error when running a class
Help please!!! Is there a method for closing sessions java-side?