Before reading the following content, I would like to ask you a question:
Will finally statements be executed?
Use the Integer. valueOf method to simulate an exception to illustrate the problem between try catch and finally.
Example 1: no exception, normal
[Java]
Public class TryCatchFinally {
Public static void main (String [] args ){
Try {
System. out. println ("try statement :-)");
Integer. valueOf ("2 ");
} Catch (NumberFormatException nfe ){
System. out. println ("catch statement :-)");
} Finally {
System. out. println ("finally statement :-)");
}
}
}
Example 2: no exception, normal, but add return in the try statement
[Java]
Public class TryCatchFinally {
Public static void main (String [] args ){
Try {
System. out. println ("try statement :-)");
Integer. valueOf ("2 ");
Return;
} Catch (NumberFormatException nfe ){
System. out. println ("catch statement :-)");
} Finally {
System. out. println ("finally statement :-)");
}
}
}
The running results of Example 1 and Example 2 are the same:
Example 3: Manufacturing exception
[Java]
Public class TryCatchFinally {
Public static void main (String [] args ){
Try {
System. out. println ("try statement :-)");
Integer. valueOf ("bluetooth ");
} Catch (NumberFormatException nfe ){
System. out. println ("catch statement :-)");
} Finally {
System. out. println ("finally statement :-)");
}
}
}
Example 4: create an exception. Add return to the catch statement.
[Java]
Public class TryCatchFinally {
Public static void main (String [] args ){
Try {
System. out. println ("try statement :-)");
Integer. valueOf ("bluetooth ");
} Catch (NumberFormatException nfe ){
System. out. println ("catch statement :-)");
Return;
} Finally {
System. out. println ("finally statement :-)");
}
}
}
The running result of Example 3 and Example 4 is the same:
From this, we can see that the finally statements are executed in the above four cases.
Example 5: create an exception. Add return to the catch statement.
Purpose: To verify whether the code behind the finally code block is executed ??????
[Java]
Public class TryCatchFinally {
Public static void main (String [] args ){
Try {
System. out. println ("try statement :-)");
Integer. valueOf ("bluetooth ");
} Catch (NumberFormatException nfe ){
System. out. println ("catch statement :-)");
Return;
} Finally {
System. out. println ("finally statement :-)");
}
System. out. println ("out of try... catch... finally statement ");
}
}
Execution result:
If you comment out the return in the catch statement, the execution result is as follows:
Example 6: create an exception. Add return to the try statement.
[Java]
Public class TryCatchFinally {
Public static void main (String [] args ){
Try {
System. out. println ("try statement :-)");
Integer. valueOf ("dd ");
Return;
} Catch (NumberFormatException nfe ){
System. out. println ("catch statement :-)");
} Finally {
System. out. println ("finally statement :-)");
}
System. out. println ("out of try... catch... finally statement ");
}
}
Execution result:
Example 7: no exception. Add return in the try statement.
[Java]
Public class TryCatchFinally {
Public static void main (String [] args ){
Try {
System. out. println ("try statement :-)");
Integer. valueOf ("2 ");
Return;
} Catch (NumberFormatException nfe ){
System. out. println ("catch statement :-)");
} Finally {
System. out. println ("finally statement :-)");
}
System. out. println ("out of try... catch... finally statement ");
}
}
Execution result (no exception, return returns, so the statement after the finally code block will not execute :-)
The above indications indicate:
All finally statements have been executed, but there is another situation that has not been verified. Next let's look at it.
Exit JVM in try... catch:
[Java]
Public class TryCatchFinally {
Public static void main (String [] args ){
Try {
System. out. println ("try statement :-)");
Integer. valueOf ("2eer ");
System. exit (0 );
} Catch (NumberFormatException nfe ){
System. out. println ("catch statement :-)");
System. exit (0 );
} Finally {
System. out. println ("finally statement :-)");
}
System. out. println ("out of try... catch... finally statement ");
}
}
Execution result:
If System. exit is called in a try or catch Statement (any one), the finally statement will not be executed.
Author: AndroidBluetooth