1 / 21

CSC111H Exceptions 2

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.

carr
Download Presentation

CSC111H Exceptions 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC111HExceptions 2 Dennis Burford dburford@cs.uct.ac.za

  2. Catching Multiple Exceptions • Say we wanted to do something like: int a = Integer.parseInt( ”25" ); test.setAge( a ); System.out.println( test.age );

  3. Catching Multiple Exceptions • Both statements generate their own exception. Is there a way we can use a single try block for both? • Yes!

  4. 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?

  5. Exception Exceptions are classes within a class hierarchy: ClassNotFoundException RunTimeException IOException ArithmeticException NullPointerException Illegal ArgumentException NumberFormatException

  6. 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)

  7. Catching Multiple Exceptions try { int a = Integer.parseInt( "ten" ); test.setAge( a ); } catch (Exception e) { System.out.println("Exception:"+e.getMessage()); }

  8. 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! {... }

  9. 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...

  10. 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() ); }

  11. 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; }

  12. 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...

  13. Creating Exception Classes class AgeException extends Exception { public AgeException() {} public AgeException( String s ) { super("Improper age: " + s ); } }

  14. Creating Exception Classes public void setAge( int a ) throws AgeException { if (a < 0) throw new AgeException("Age must be > 0"); age = a; }

  15. 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.

  16. 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 // .... }

  17. 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

  18. 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."); }

  19. Result: Improper age: Age must be > 0 End of try-catch code. 0

  20. ?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?

  21. 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.

More Related