//---------------------------------------- // Demonstration of exception handling //---------------------------------------- public class CaughtExceptions { public static void main (String[] args) throws IntegerOverflowException { int x = 10; int y = 2; int[] z = {10, 20, 30, 40 ,50}; String w = "Hello World"; Divide t = null; int maxint = 2147483647; try { System.out.println (x/y); // possible division by zero System.out.println (z[4]); // possible array index out of bounds System.out.println (w.charAt(10)); // possible srting index out of bounds t = new Divide(5,2); // possible exception propagation (division by zero) System.out.println (t.getValue()); // possible null pointer reference System.out.println(new Add(maxint,1).getValue()); // demonstrates defining a new exception } catch (NullPointerException e) { System.out.println (e); } catch (StringIndexOutOfBoundsException e) { System.out.println (e); } catch (ArrayIndexOutOfBoundsException e) { System.out.println (e); } catch (ArithmeticException e) { System.out.println (e); } catch (IntegerOverflowException e) { System.out.println (e); } finally { System.out.println ("This text will not be printed if an exception occurs"); } } }