Java Exception description displaying

Introduction

Throwable overrides the toString() method defined by Object.

It returns a string containing a description of the exception.

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

    try { // monitor a block of code.
      d = 0;//w  w  w.  j a v a 2  s .  com
      a = 42 / d;
      System.out.println("This will not be printed.");
    } catch (ArithmeticException e) { // catch divide-by-zero error
      System.out.println("Exception: " + e);  
      a = 0; // set a to zero and continue  
    }
    System.out.println(a);
  }
}



PreviousNext

Related