210 likes | 302 Views
CSC111H Exceptions 2. Dennis Burford dburford@cs.uct.ac.za. Catching Multiple Exceptions. Say we wanted to do something like: int a = Integer.parseInt( ”25" ); test.setAge( a ); System.out.println( test.age );. Catching Multiple Exceptions.
E N D
CSC111HExceptions 2 Dennis Burford dburford@cs.uct.ac.za
Catching Multiple Exceptions • Say we wanted to do something like: int a = Integer.parseInt( ”25" ); test.setAge( a ); System.out.println( test.age );
Catching Multiple Exceptions • Both statements generate their own exception. Is there a way we can use a single try block for both? • Yes!
Catching Multiple Exceptions try { int a = Integer.parseInt( "ten" ); test.setAge( a ); } catch (Exception e) { System.out.println("Exception:"+e.getMessage()); } • But how does this work?
Exception Exceptions are classes within a class hierarchy: ClassNotFoundException RunTimeException IOException ArithmeticException NullPointerException Illegal ArgumentException NumberFormatException
Exception Classes and Hierarchy • When we catch an exception of type “Exception”, we are using the original base-class and will therefore catch all classes that inherit from it. That is, all exceptions. • Every exception class “is-an” Exception (e.g. NumberFormatException is an Exception)
Catching Multiple Exceptions try { int a = Integer.parseInt( "ten" ); test.setAge( a ); } catch (Exception e) { System.out.println("Exception:"+e.getMessage()); }
Exception Classes and Hierarchy • Is this good? We can have a single try-catch pair and catch every possible exception in our program. try {... } catch (Exception e) // Catch all! {... }
Exception Classes and Hierarchy • No. Using a single catch (Exception e) for everything may save us time, but it’s not good programming. • We may need to take different action, depending on the problem. • Fortunately, there is a better way...
Catching Multiple Exceptions try { int a = Integer.parseInt( "ten" ); test.setAge( a ); } catch (NumberFormatException ne) { S.o.pln("NumberFormatException:"+ne.getMessage(); } catch (Exception e) // catch all { S.o.pln( "Exception: "+e.getMessage() ); }
Catching Multiple Exceptions • This is a little like the switch statement, with its default case at the end: switch ( num ) { case 0: S.o.pln( “zero”); break; case 1: S.o.pln( “one”); break; default: S.o.pln( “Some other number”); break; }
Creating Exception Classes • So, if Exceptions are classes, can I create my own? • Yes. Simply inherit from Exception, or one of its sub-classes. • For example, we can have a special exception for our age problem...
Creating Exception Classes class AgeException extends Exception { public AgeException() {} public AgeException( String s ) { super("Improper age: " + s ); } }
Creating Exception Classes public void setAge( int a ) throws AgeException { if (a < 0) throw new AgeException("Age must be > 0"); age = a; }
Creating Exception Classes • One advantage of creating your own exception class is that you can add data members and methods that can help you correct the problem. • Another advantage is that you can now catch your exception explicitly and do any specific actions that are required.
Creating Exception Classes try { int a = Integer.parseInt( "-10" ); test.setAge( a ); } catch (NumberFormatException ne) { S.o.pln("NumberFormatException:"+ne.getMessage() ); } catch (AgeException ae) { S.o.pln( ae.getMessage() ); } catch (Exception e) { // catch all others // .... }
Finally... • There is one last part to the try-catch structure… • We can have a finally block after the last catch which will execute its code even if an exception occurs. • This is useful if we want to clean up after ourselves. Use the finally block to make sure code is executed before a method is finished
int a; try { a = Integer.parseInt( "-10" ); test.setAge( a ); } catch (NumberFormatException ne) { ... } catch (AgeException ae) { ... } catch (Exception e) { // catch all others .... } finally { a = 0; S.o.pln("End of try-catch code."); }
Result: Improper age: Age must be > 0 End of try-catch code. 0
?Brain-Teaser? • Why does the compiler complain if we don’t have try-catch around setAge(), but its ok when we just have Integer.parseInt() without try-catch?
Answer • Exceptions are classes with a class hierarchy. NumberFormatException inherits from RunTimeException - a special type of Exception that does not have to be caught or thrown. If it occurs, it will simply crash the program. Effectively, every method automatically throws a RunTimeException.