try{...}catch(Exception e) {...}finally{...}
puzzle.Before running the code ask yourself three questions: does it compile, does it run, and what is the result?
Tell me what the expected result is supposed to be and why?
Main.java
The example code is a NetBeans project and can be downloaded here: FinallyReturn.zip
It should print out the message - you caught the first exception, so the catch block executes, and the finally block always executes so it'll print the message.
ReplyDeleteYes, it did print the message. Did you notice anything else?
ReplyDeleteNo exception gets thrown because of the return in the finally.
ReplyDeleteAnd the "NullPointerException 2" is never catched...
ReplyDeleteThe return statement causes the Thread private method exit() to be cause which clears several references including the one for uncaughtExceptionHandler, so no exceptions will be caught after the main method exits.
ReplyDelete