import java.lang.Exception; public class TryCatch { public static void trigger (int i) throws ArrayIndexOutOfBoundsException, Exception { if (i==1) throw new ArrayIndexOutOfBoundsException ("Specific exception"); if(i==2) throw new Exception ("Generic exception"); } public static void main (String args[]) { double x[] = new double[10]; int i = 10; try { x [i] = 10. ; } catch (Exception e) { System.out.println ("Found Array out of bounds"); //If you want it to be fatal, uncomment the next line //System.exit(0); } //you can also get specific exceptions try { x[i] = 10.0; } catch (ArrayIndexOutOfBoundsException ae) { System.out.println ("A more specific exception"); } //trigger() will throw an exception if asked to try { trigger(1); } catch (ArrayIndexOutOfBoundsException e) { System.out.println ("Found a type 1 error in trigger"); } catch (Exception e) { System.out.println ("Found a type 2 error in trigger"); } try { trigger(2); } catch (ArrayIndexOutOfBoundsException e) { System.out.println ("Found a type 1 error in trigger"); } catch (Exception e) { System.out.println ("Found a type 2 error in trigger"); //e.printStackTrace(); } } }