890 likes | 1.04k Views
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.
E N D
COP 3503 FALL 2012ShayanJavedLecture 11 Programming Fundamentals using Java
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. • Detected by the compiler • javac for Java • g++ for C++
Logic Errors • Program compiles and runs, but results are wrong.
Logic Errors • Program compiles and runs, but results are wrong. • Detected and fixed through testing.
Logic Errors • Program compiles and runs, but results are wrong. • Detected and fixed through testing. • Arise because logic coded by the programmer was incorrect.
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
Runtime Errors • Occur when program is running – environment detects it and can’t carry it out
Runtime Errors • Occur when program is running – environment detects it and can’t carry it out • Examples of Code Errors: • Divide by zero
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
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)
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
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
Errors int[] numbers = { 1.5, 5, 7 }; System.out.prntln(numbers[numbers.length]); Try to point out all the errors in this code
Errors int[] numbers = { 1.5, 5, 7 }; System.out.prntln(numbers[numbers.length]); Syntax Error(s)
Errors int[] numbers = { 1.5, 5, 7 }; System.out.prntln(numbers[numbers.length]); Syntax Error(s)
Errors int[] numbers = { 1.5, 5, 7 }; System.out.prntln(numbers[numbers.length]); Syntax Error(s) Runtime Error(s)
Exception An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions.
Exception Handling in Java • Mechanism for handling exceptions by detecting and responding to them in a systematic, uniform and reliable manner.
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)
Exception • A method in Java throws Exceptions • “Something went wrong”
Exception • A method in Java throws Exceptions • “Something went wrong” • Exceptions are Objects • Every Exception is a subclass of the Exception class
System Errors • Thrown by the Java Virtual Machine (JVM)
System Errors • Thrown by the Java Virtual Machine (JVM) • Represented by the Error class
System Errors • Thrown by the Java Virtual Machine (JVM) • Represented by the Error class • Describes internal system errors
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
Checked Exceptions Need to explicitly deal with Checked Exceptions: try and catch them, or throw them
Exception Handling • Keywords: • try some code, catch any Exceptions
Exception Handling • Keywords: • try some code, catch any Exceptions • or throw an Exception
Exception Handling • Keywords: • try some code, catch any Exceptions • or throw an Exception • finally execute some code
Exception Handling • Java forces you to deal with checked Exceptions
Exception Handling • Java forces you to deal with checked Exceptions • Two ways to deal with them: void p1 () { try { riskyMethod(); } catch (Exception ex) { .... } } (a)
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)
Exception Handling • Remember the clone() method?
Exception Handling • Remember the clone() method? • Can be written in two ways: Object clone() { try { return super.clone(); } catch (CloneNotSupportedException ex) { .... } } (a)
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)
Exception Handling • In the first case, we are catching and handling the Exception
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
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
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
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
Catching Exceptions • ThrownExceptions have to be eventually caught somewhere in your code
Exception Information • So an Exception has been caught – what can we do with it? try { // Statements which throw Exceptions } catch (Exceptionexception) { // ALL Exceptions }
Exception Information • Some useful methods in the Throwable class: