Exception Handling

An exception is a run-time error.

Java exception handling is managed via five keywords: try, catch, throw, throws, and finally.

Program statements that might have exceptions are contained within a try block. The exception handler is coded using catch statement

To manually throw an exception, use the keyword throw. Any exception that is thrown out of a method must be specified by a throws clause.

Any code that would be executed regardless after a try block is put in a finally block.

This is the general form of an exception-handling block:

try { 
// block of code to monitor for errors 
catch (ExceptionType1 exOb) { 
// exception handler for ExceptionType1 
catch (ExceptionType2 exOb) { 
// exception handler for ExceptionType2 
// ... 
finally { 
// block of code to be executed after try block ends 

Here, ExceptionType is the type of exception that has occurred.

  Java Book 
    Language Basics  

Exception Handler:
  1. Exception Handling
  2. Exception Types
  3. try and catch
  4. Displaying a Description of an Exception
  5. Multiple catch Clauses
  6. Nested try Statements
  7. Creates and throws an exception
  8. Methods with throws clause
  9. finally
  10. Java's Built-in Exceptions
  11. Creating Your Own Exception Subclasses
  12. Chained Exceptions