public class TryThrowCatchFinally1 { public static void main(String[] args) { try { final double dividend = Double.MAX_VALUE; final double divisor = 2 * Math.random(); if (divisor == 0.0) { DivideByZero dividebyzero = new DivideByZero(); throw dividebyzero; /* Could write the above as throw new DivideByZero(); */ } if (divisor < 1.0) { throw new OverFlow(); } System.out.println(dividend/divisor); } catch (DivideByZero d) { System.err.println(d.getMessage()); } catch (OverFlow ov) { System.err.println(ov.getMessage()); } finally { //Arrive here if no exception was thrown, //or if DivideByZero or OverFlow was thrown, //or if any other excpetion was thrown. System.out.println("All done."); } } } /* Class DividebyZero is derived from class Exception. An object of class Exception can hold a string, and has a method named getMessage. */ class DivideByZero extends Exception { public DivideByZero() { //constructor super("can't divide by zero"); } } class OverFlow extends Exception { public OverFlow() { super("quotient would overflow"); } }