Exceptions • Handling exceptions • Exceptions are objects – Subclasses of java.lang.Exception – Checked and unchecked exceptions • Exception propagation • Handling multiple exceptions • Throwing an exception • Customized exception subclass • Section 8.3 in Eck textbook User enters data as a string. You need to convert to int value. String s = …. int i = Integer.parseInt( s); Javadoc for parseInt method say it throws a NumberFormatException Exceptions • Exceptions are a way to signal an error. • Exceptions extend from java.lang.Exception class. • Common methods – getMessage() – printStackTrace() • Checked exceptions must be declared in the method declaration and must be handled. – NumberFormatException, IOException • Unchecked exceptions (extend from java.lang.RuntimeException) do not. – NullPointerException, IndexOutOfBoundsException Exception Propagation • You are writing a method B that calls another method C that throws a checked exception. • You must either handle the exception in your method (try/catch) or • You must declare that your method throws the unhandled exception – If method A calls method B, then A must handle the exception or declare it. Exception Propagation OPTION 1 OPTION 2 public int convertToInt (String s) { public int convertToInt (String s) int i=0; throws NumberFormatException { try { int i = Integer.parseInt( s); i = Integer.parseInt( s); return i; } catch (NumberFormatException e) { } System.out.println( “User input invalid.”); System.out.println( e.getMessage() ); e.printStackTrace(); } return i; } Handling Multiple Exceptions public int convertToInt (String[] s) { int i=0; try { i = Integer.parseInt( s[1] ); } catch (NumberFormatException e) { System.out.println( “User input invalid.”); } catch (NullPointerException e) { System.out.println(“ Program error.”); } catch (IndexOutOfBoundsException e) { System.out.println(“ Program error.”); } catch (Exception e) { System.out.println(“ Unknown error has occurred.”); System.out.println(e.getMessage()); } return i; } Order of catch blocks is important. The code will not work correctly if this is first. Throwing an exception public double quadraticFormula (double a, double b, double c) throws Exception { double dis = b*b – 4.0*a*c; if (dis < 0) { Exception e = new Exception(“No real solution.”); throw e; } else { double r = ( -b + Math.sqrt(dis)) / (2.0*a); return r; } } Creating an exception subclass public class NoRealSolution extends Exception { private double d; public double quadraticFormula ( double a, double b, double c) throws NoRealSolution { public NoRealSolution( double value ) { super(“No Real Solution.”); d = value; } double dis = b*b – 4.0*a*c; if (dis < 0) { NoRealSolution e = new NoRealSolution(dis); throw e; } else { public double getDiscriminatorValue() { return d; } double r = ( -b + Math.sqrt(dis)) / (2.0*a); return r; } } } Handling NoRealSolution Exception double a, b, c; try { double r = x.quadraticFormula(a,b,c); System.out.println (“ zero value is “+r); } catch (NoRealSolution e) { System.out.println(“No real roots. Discriminator is negative. “ + e.getDiscriminatorValue() ); }