Software Development

Java – how to not execute the finally clause

java Sort Enum Properties Monitor Directory File Lambda Expression parameter return types Validate XML String XSD ExecutorService AutoCloseable Try-With-Resources Convert LocalDateTime OffsetDateTime Run Codes Before Shutdown XML Tag Lambda PostgreSQL JDBC invoke JavaScript Applet SFTP Batch hang block main thread MySQL JDBC try-with-resources delete Unencrypted SoapFault Apache CXF

They say that when an Exception occurs, the finally clause, if available, always executes no matter what except for one thing.

The statement at line 11 will always execute whether or not Exception occurred. The codes will output the following.

Skip Finally

This statement will skip the finally clause and terminate the whole JVM.

If we put this statement in the catch clause just after the e.printStackTrace();, we will not see the word “Finally!” displayed.

This outputs only this

Got comments, or suggestions? Please visit our Facebook page!

You Might Also Like