Exceptions

Date: 1/28/2002

try

This block wraps code that might throw an exception.

catch

This block catches the named exception that was thrown.

You may have as many catch blocks for a try as you like.

Some exceptions are required to be caught, others are not.

finally

This block of code will always execute, whether an exception is thrown or not. When an exception is thrown, the JVM first looks for a catch for that exception.  If it is found, it will execute that code.

exception thrown

catch found?

yes: 

execute code

finally clause?

yes: execute code

no: exit method

no: exit method

exception not thrown

finally clause?

yes: execute code

code outside of try-catch?

yes: execute code

Put your clean-Up code goes here. Set objects to null, mainly.

throws

There are times you do not want to catch exceptions.  Instead, you pass them up the chain.