The try keyword is used to enclose blocks of statements that might throw exceptions.
try
{
<block that may throw exceptions>
}
catch (<java.lang.Exception or subclass> e)
{
<code to handle exception e>
}
try
{
<block that may throw different exceptions>
}
catch (FooException e)
{
<code to handle FooException e>
}
catch (BarException e)
{
<code to handle BarException e>
}
try
{
<block that may throw exceptions>
}
catch (<java.lang.Exception or subclass> e)
{
<code to handle exception e>
}
finally
{
<statements that execute with or without exception>
}
Every try block must have at least one catch or finally clause.
If a particular exception class is not handled by any catch clause, the exception propagates up the call stack to the next enclosing try block, recursively. If an exception is not caught by any enclosing try block, the Java interpretor will exit with an error message and stack trace.