1 / 15

CSE 1341 Principles of Computer Science I

CSE 1341 Principles of Computer Science I. Spring 2008 Mark Fontenot mfonten@engr.smu.edu. Note Set 10. Note Set 10 Overview. Exception Handling. exception. Exception – an indication of a problem that occurs during a program’s execution

ike
Download Presentation

CSE 1341 Principles of Computer Science I

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. CSE 1341 Principles of Computer Science I Spring 2008 Mark Fontenot mfonten@engr.smu.edu Note Set 10

  2. Note Set 10Overview Exception Handling

  3. exception • Exception – an indication of a problem that occurs during a program’s execution • Exception handling allows the graceful handling of errors in a program. • Goal – allow the program to continue executing as if no problem had been encountered • Can somewhat separate error handling code from regular processing code

  4. The standard Do Something (processing) Check to see if it worked (error checking) Do something else Check to see if it worked Do Something Check to see if it worked

  5. Read input; exception occurs if input is not a valid integer Attempt to divide; denominator may be zero

  6. Stack Trace

  7. Exception Handling Code try { //put code here that might // throw an exception //if exception is thrown, move to catch } catch (InputMismatchException e) { //Handle the exception case here – do what is //needed to gracefully recover from the //exception } catch (Exception e) { //Can handle multiple types of exceptions } Try Block Exception handlers Exception Parameter Uncaught exception – exception for which there is no exception handler - exception type doesn’t match any of the catch blocks

  8. ExceptionTest public class ExceptionTest { public static void main (String [] args) { int x = 3; int y = 0; int z = x / y; System.out.println(z); } } throws ArithmeticException run: Exception in thread "main" java.lang.ArithmeticException: / by zero at ExceptionTest.main(ExceptionTest.java:9) Java Result: 1

  9. ExceptionTest2 public class ExceptionTest2 { public static void main (String [] args) { int x = 3; int y = 0; int z = 0; try { z = x / y; } catch (ArithmeticException e) { System.out.println("Exception Caught"); } System.out.println(z); } } run-single: Exception Caught 0

  10. With Scanner import java.util.*; public class ExceptionTest3 { public static void main (String [] args) { Scanner s = new Scanner(System.in); int x = 0; int y = 0; try { x = s.nextInt(); } catch (InputMismatchException e) { e.printStackTrace(); } } }

  11. Use Java API JAVA API lists the exceptions thrown by each method

  12. Termination Model of Exception Handling try { code line 1 code line 2 code line 3 } catch (Exception e) { handler 1 handler 2 } code line 4 code line 5 if line 2 throws exception, control transfers to exception handler then to line of code after handlers.

  13. Objectville All exception classes directly or indirectly extend java.lang.Exception. It is possible to create your own exception classes by extending java.lang.Exception.

  14. Objectville • Unchecked Exceptions • Subclass of RuntimeException • Not required to be handled • Checked Exceptions • Subclass of Exception • Must be handled or explicitlyor declared w/ throws

  15. throws public intmyFunction () throws MySpecialException{ //Code here that could throw a MySpecialException //but you don’t want to handle it here for some reason //so you have to declare that this method could also throw //MySpecialException – a checked exception }

More Related