Mega Code Archive

 
Categories / Java Book / 001 Language Basics
 

0102 Displaying a Description of an Exception

You can display exception description message in a println( ) statement. For example, the catch block can be written like this: import java.util.Random; public class Main { public static void main(String args[]) { int a = 0, b = 0, c = 0; Random r = new Random(); for (int i = 0; i < 32000; i++) { try { b = r.nextInt(); c = r.nextInt(); a = 12345 / (b / c); } catch (ArithmeticException e) { System.out.println("Exception: " + e); a = 0; // set a to zero and continue } System.out.println("a: " + a); } } }