Multiple Catch

  • A single try block can have many catch blocks.

  • When the try block has statements that pick up different types of exceptions.

  • try {
    }
    catch (IllegalArgumentException e) {
    some code;
    }
    catch (SecurityException e) {
    someCode();
    }
    catch (IllegalAccessException e) {
    someCode();
    }
    catch (NoSuchFieldException e) {
    someCode();
    }

Nested Try

  • A try statement can be inside block of another try.

  • Sometimes Condition may arise where part of block may cause one error and entire block may cause another error.So we can use in java, nested try.

  • try {
    statement 1;
    statement 2;
    try {
    statement 1;
    statement 2;
    }
    catch (Exception1 e)
    {
    //statements to handle the exception
    }
    }
    catch (Exception2 e2)
    {
    //statements to handle the exception
    }
  • When nested try blocks used,inner try block is executed first.

  • Any Exception thrown in the inner try block is caught the corresponding catch block.

  • If matching catch block is not found So catch block of outer block are examining until all nested try statement exhausted.

  • If no matching blocks are found,So java Runtime Ennvironment handles the execution.