Catching All Exceptions at Once - Java Object Oriented Design

Java examples for Object Oriented Design:Exception

Introduction

Java provides a catch-all exception class called Exception that all other types of exceptions are based on.

Use Exception class instead of a more specific exception class.

For example:

try
{
      int c = a / b;
}
catch (Exception e)
{
     System.out.println("Oops, you can't divide by
           zero.");
}

In this example, the catch block specifies Exception rather than ArithmeticException.

The following code provides specific processing for various exception types from the try statement.

try
{
      // statements that might throw several types of
    // exceptions
}
catch (InputMismatchException e)
{
      // statements that process InputMismatchException
}
catch (IOException e)
{
      // statements that process IOException
}
catch (Exception e)
{
      // statements that process all other exception types
}

Related Tutorials