1 / 89

COP 3503 FALL 2012 Shayan Javed Lecture 11

COP 3503 FALL 2012 Shayan Javed Lecture 11. Programming Fundamentals using Java. Exception Handling. Errors. Syntax Errors Logic Errors Runtime Errors. Syntax Errors. Arise because language rules weren’t followed. Syntax Errors. Arise because language rules weren’t followed.

deva
Télécharger la présentation

COP 3503 FALL 2012 Shayan Javed Lecture 11

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. COP 3503 FALL 2012ShayanJavedLecture 11 Programming Fundamentals using Java

  2. Exception Handling

  3. Errors • Syntax Errors • Logic Errors • Runtime Errors

  4. Syntax Errors • Arise because language rules weren’t followed.

  5. Syntax Errors • Arise because language rules weren’t followed. • Detected by the compiler • javac for Java • g++ for C++

  6. Logic Errors • Program compiles and runs, but results are wrong.

  7. Logic Errors • Program compiles and runs, but results are wrong. • Detected and fixed through testing.

  8. Logic Errors • Program compiles and runs, but results are wrong. • Detected and fixed through testing. • Arise because logic coded by the programmer was incorrect.

  9. Logic Errors • Program compiles and runs, but results are wrong. • Detected and fixed through testing. • Arise because logic coded by the programmer was incorrect. • Example: wrote c = a - b instead of c = a + b

  10. Runtime Errors • Occur when program is running – environment detects it and can’t carry it out

  11. Runtime Errors • Occur when program is running – environment detects it and can’t carry it out • Examples of Code Errors: • Divide by zero

  12. Runtime Errors • Occur when program is running – environment detects it and can’t carry it out • Examples of Code Errors: • Divide by zero • Array out of bounds

  13. Runtime Errors • Occur when program is running – environment detects it and can’t carry it out • Examples of Code Errors: • Divide by zero • Array out of bounds • Accessing a null pointer (reference)

  14. Runtime Errors • Occur when program is running – environment detects it and can’t carry it out • Examples of Code Errors: • Divide by zero • Array out of bounds • Accessing a null pointer (reference) • Integer overflow

  15. Runtime Errors • Occur when program is running – environment detects it and can’t carry it out • Examples of Code Errors: • Divide by zero • Array out of bounds • Accessing a null pointer (reference) • Integer overflow • Programs crash when such exceptions are not handled

  16. Errors int[] numbers = { 1.5, 5, 7 }; System.out.prntln(numbers[numbers.length]); Try to point out all the errors in this code

  17. Errors int[] numbers = { 1.5, 5, 7 }; System.out.prntln(numbers[numbers.length]); Syntax Error(s)

  18. Errors int[] numbers = { 1.5, 5, 7 }; System.out.prntln(numbers[numbers.length]); Syntax Error(s)

  19. Errors int[] numbers = { 1.5, 5, 7 }; System.out.prntln(numbers[numbers.length]); Syntax Error(s) Runtime Error(s)

  20. Exception An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions.

  21. Exception Handling in Java • Mechanism for handling exceptions by detecting and responding to them in a systematic, uniform and reliable manner.

  22. Exception Handling in Java • Mechanism for handling exceptions by detecting and responding to them in a systematic, uniform and reliable manner. • Any exceptions not handled within the Java program are “caught” by the Java Runtime Environment (JRE)

  23. Exception • A method in Java throws Exceptions • “Something went wrong”

  24. Exception • A method in Java throws Exceptions • “Something went wrong” • Exceptions are Objects • Every Exception is a subclass of the Exception class

  25. Unchecked Exceptions/Errors

  26. System Errors

  27. System Errors • Thrown by the Java Virtual Machine (JVM)

  28. System Errors • Thrown by the Java Virtual Machine (JVM) • Represented by the Error class

  29. System Errors • Thrown by the Java Virtual Machine (JVM) • Represented by the Error class • Describes internal system errors

  30. System Errors • Thrown by the Java Virtual Machine (JVM) • Represented by the Error class • Describes internal system errors • Rarely occur – if they do you can’t do much other than terminating

  31. Runtime Exceptions (Unchecked)

  32. Checked Exceptions

  33. Checked Exceptions Need to explicitly deal with Checked Exceptions: try and catch them, or throw them

  34. Exception Handling • Keywords: • try some code, catch any Exceptions

  35. Exception Handling • Keywords: • try some code, catch any Exceptions • or throw an Exception

  36. Exception Handling • Keywords: • try some code, catch any Exceptions • or throw an Exception • finally execute some code

  37. Exception Handling • Java forces you to deal with checked Exceptions

  38. Exception Handling • Java forces you to deal with checked Exceptions • Two ways to deal with them: void p1 () { try { riskyMethod(); } catch (Exception ex) { .... } } (a)

  39. Exception Handling • Java forces you to deal with checked Exceptions • Two ways to deal with them: void p1 () { try { riskyMethod(); } catch (Exception ex) { .... } } (a) void p1 () throws Exception { riskyMethod(); } (b)

  40. Exception Handling • Remember the clone() method?

  41. Exception Handling • Remember the clone() method? • Can be written in two ways: Object clone() { try { return super.clone(); } catch (CloneNotSupportedException ex) { .... } } (a)

  42. Exception Handling • Remember the clone() method? • Can be written in two ways: Object clone() { try { return super.clone(); } catch (CloneNotSupportedException ex) { .... } } (a) Object clone() throws CloneNotSupportedException { return super.clone(); } (b)

  43. Exception Handling • In the first case, we are catching and handling the Exception

  44. Exception Handling • In the first case, we are catching and handling the Exception • In the second case we are throwing it – needs to be caught and handled by the calling method

  45. Catching Exceptions • A try-catch statement: try { // Statement(s) which throw Exceptions } catch (Exception1exception1) { // Handles Exceptions of type Exception1 } catch (Exception2exception2) { // Handles Exceptions of type Exception2 } catch (Exceptionexception) { // Handles Exceptions of type Exception // ALL Exceptions } // Any code after the try-catch block

  46. Catching Exceptions • A try-catch statement: try { // Statement(s) which throw Exceptions } catch (CloneNotSupportedException exception1) { // Handles Exceptions of type CloneNotSupportedException } catch (NullPointerException exception2) { // Handles Exceptions of type NullPointerException } catch (Exceptionexception) { // Handles Exceptions of type Exception // ALL Exceptions } // Any code after the try-catch block

  47. Catching Exceptions • A try-catch statement: try { Circle clone = circle1.clone(); } catch (CloneNotSupportedException exception1) { // Handles Exceptions of type CloneNotSupportedException } catch (NullPointerException exception2) { // Handles Exceptions of type NullPointerException } catch (Exceptionexception) { // Handles Exceptions of type Exception // ALL Exceptions } // Any code after the try-catch block

  48. Catching Exceptions • ThrownExceptions have to be eventually caught somewhere in your code

  49. Exception Information • So an Exception has been caught – what can we do with it? try { // Statements which throw Exceptions } catch (Exceptionexception) { // ALL Exceptions }

  50. Exception Information • Some useful methods in the Throwable class:

More Related