Catching Exceptions

try
{
   // Code which throws exceptions
} catch (final IOException e)
{
   // Exception handling
}

Finally

try
{
   // Code which throws exceptions
} catch (final IOException e)
{
   // Exception handling
} finally {
   // Always runs
}

Try With Resources

To make sure the resources are always closed or finalized there is the try-with-resources variant, since Java 7:

The istream will be closed no matter what happens, without explicit code. All this requires is that the resources implement the AutoCloseable interface.

Chaining Exceptions

It is possible to answer an exception with another:

In this case a checked exception is transformed into a runtime exception. These are chained exceptions, and Throwable offers methods to travel through the chain or exceptions.

More Information

Last updated

Was this helpful?