Java Exception try and catch

Introduction

This is the general form of a try catch block:

try {  /*from  w ww.j  av  a2  s  . com*/
// 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  
} 

The following program includes a try block and a catch clause that processes the ArithmeticException generated by the division-by-zero error:

public class Main {
  public static void main(String args[]) {
    int d, a = 0;

    try { // monitor a block of code.
      d = 0;//from   www.jav a  2  s.co m
      a = 42 / d;
      System.out.println("This will not be printed.");
    } catch (ArithmeticException e) { // catch divide-by-zero error
      System.out.println("Division by zero.");
    }
    System.out.println("After catch statement.");
  }
}

This program generates the following output:

Division by zero.
After catch statement.

Handle an exception and move on.

public class Main {
  public static void main(String args[]) {
    int d, a = 0;

    try { // monitor a block of code.
      d = 0;//from  w  w  w.j a v a  2  s.co m
      a = 42 / d;
      System.out.println("This will not be printed.");
    } catch (ArithmeticException e) { // catch divide-by-zero error
      System.out.println("Division by zero.");
    }
    
    System.out.println("Program can continue");

    d = 7;
    a = 42 / d;

    System.out.println(a);
  }
}


public class Main {
  public static void main(String[] args) {
    try {/* ww w  .j  av  a 2s .  c om*/
      throwException();
    } catch (Exception exception) // exception thrown by throwException
    {
      System.err.println("Exception handled in main");
    }

    doesNotThrowException();
  }

  // demonstrate try...catch...finally
  public static void throwException() throws Exception {
    try // throw an exception and immediately catch it
    {
      System.out.println("Method throwException");
      throw new Exception(); // generate exception
    } catch (Exception exception) // catch exception thrown in try
    {
      System.err.println("Exception handled in method throwException");
      throw exception; // rethrow for further processing

      // code here would not be reached; would cause compilation errors

    } finally // executes regardless of what occurs in try...catch
    {
      System.err.println("Finally executed in throwException");
    }

    // code here would not be reached; would cause compilation errors

  }

  // demonstrate finally when no exception occurs
  public static void doesNotThrowException() {
    try // try block does not throw an exception
    {
      System.out.println("Method doesNotThrowException");
    } catch (Exception exception) // does not execute
    {
      System.err.println(exception);
    } finally // executes regardless of what occurs in try...catch
    {
      System.err.println("Finally executed in doesNotThrowException");
    }

    System.out.println("End of method doesNotThrowException");
  }
}



PreviousNext

Related