1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.
-
Upload
jillian-burleigh -
Category
Documents
-
view
218 -
download
0
Transcript of 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.
![Page 1: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/1.jpg)
1 / 89
COP 3503 FALL 2012SHAYAN JAVED
LECTURE 11
Programming Fundamentals using Java
1
![Page 2: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/2.jpg)
2 / 89
Exception Handling
![Page 3: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/3.jpg)
3 / 89
Errors
Syntax Errors
Logic Errors
Runtime Errors
![Page 4: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/4.jpg)
4 / 89
Syntax Errors
Arise because language rules weren’t followed.
![Page 5: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/5.jpg)
5 / 89
Syntax Errors
Arise because language rules weren’t followed.
Detected by the compiler javac for Java g++ for C++
![Page 6: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/6.jpg)
6 / 89
Logic Errors
Program compiles and runs, but results are wrong.
![Page 7: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/7.jpg)
7 / 89
Logic Errors
Program compiles and runs, but results are wrong. Detected and fixed through testing.
![Page 8: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/8.jpg)
8 / 89
Logic Errors
Program compiles and runs, but results are wrong. Detected and fixed through testing.
Arise because logic coded by the programmer was incorrect.
![Page 9: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/9.jpg)
9 / 89
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
![Page 10: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/10.jpg)
10 / 89
Runtime Errors
Occur when program is running – environment detects it and can’t carry it out
![Page 11: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/11.jpg)
11 / 89
Runtime Errors
Occur when program is running – environment detects it and can’t carry it out
Examples of Code Errors: Divide by zero
![Page 12: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/12.jpg)
12 / 89
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
![Page 13: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/13.jpg)
13 / 89
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)
![Page 14: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/14.jpg)
14 / 89
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
![Page 15: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/15.jpg)
15 / 89
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
![Page 16: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/16.jpg)
16 / 89
Errors
int[] numbers = { 1.5, 5, 7 };
System.out.prntln(numbers[numbers.length]);
Try to point out all the errors in this code
![Page 17: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/17.jpg)
17 / 89
Errors
int[] numbers = { 1.5, 5, 7 };
System.out.prntln(numbers[numbers.length]);
Syntax Error(s)
![Page 18: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/18.jpg)
18 / 89
Errors
int[] numbers = { 1.5, 5, 7 };
System.out.prntln(numbers[numbers.length]);
Syntax Error(s)
![Page 19: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/19.jpg)
19 / 89
Errors
int[] numbers = { 1.5, 5, 7 };
System.out.prntln(numbers[numbers.length]);
Syntax Error(s)
Runtime Error(s)
![Page 20: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/20.jpg)
20 / 89
Exception
An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions.
![Page 21: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/21.jpg)
21 / 89
Exception Handling in Java
Mechanism for handling exceptions by detecting and responding to them in a systematic, uniform and reliable manner.
![Page 22: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/22.jpg)
22 / 89
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)
![Page 23: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/23.jpg)
23 / 89
Exception
A method in Java throws Exceptions “Something went wrong”
![Page 24: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/24.jpg)
24 / 89
Exception
A method in Java throws Exceptions “Something went wrong”
Exceptions are Objects Every Exception is a subclass of the Exception class
![Page 25: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/25.jpg)
25 / 89
Unchecked Exceptions/Errors
LinkageError
Error
AWTError
AWTException
Throwable
ClassNotFoundException
VirtualMachineError
IOException
Exception
RuntimeException
Object
ArithmeticException
NullPointerException
IndexOutOfBoundsException
Several more classes
Several more classes
Several more classes
IllegalArgumentException
![Page 26: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/26.jpg)
26 / 89
System Errors
LinkageError
Error
AWTError
AWTException
Throwable
ClassNotFoundException
VirtualMachineError
IOException
Exception
RuntimeException
Object
ArithmeticException
NullPointerException
IndexOutOfBoundsException
Several more classes
Several more classes
Several more classes
IllegalArgumentException
![Page 27: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/27.jpg)
27 / 89
System Errors
Thrown by the Java Virtual Machine (JVM)
![Page 28: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/28.jpg)
28 / 89
System Errors
Thrown by the Java Virtual Machine (JVM)
Represented by the Error class
![Page 29: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/29.jpg)
29 / 89
System Errors
Thrown by the Java Virtual Machine (JVM)
Represented by the Error class
Describes internal system errors
![Page 30: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/30.jpg)
30 / 89
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
![Page 31: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/31.jpg)
31 / 89
Runtime Exceptions (Unchecked)
LinkageError
Error
AWTError
AWTException
Throwable
ClassNotFoundException
VirtualMachineError
IOException
Exception
RuntimeException
Object
ArithmeticException
NullPointerException
IndexOutOfBoundsException
Several more classes
Several more classes
Several more classes
IllegalArgumentException
![Page 32: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/32.jpg)
32 / 89
Checked Exceptions
LinkageError
Error
AWTError
AWTException
Throwable
ClassNotFoundException
VirtualMachineError
IOException
Exception
RuntimeException
Object
ArithmeticException
NullPointerException
IndexOutOfBoundsException
Several more classes
Several more classes
Several more classes
IllegalArgumentException
![Page 33: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/33.jpg)
33 / 89
Checked Exceptions
LinkageError
Error
AWTError
AWTException
Throwable
ClassNotFoundException
VirtualMachineError
IOException
Exception
RuntimeException
Object
ArithmeticException
NullPointerException
IndexOutOfBoundsException
Several more classes
Several more classes
Several more classes
IllegalArgumentException
Need to explicitly deal with Checked Exceptions:
try and catch them, or throw them
![Page 34: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/34.jpg)
34 / 89
Exception Handling
Keywords: try some code, catch any Exceptions
![Page 35: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/35.jpg)
35 / 89
Exception Handling
Keywords: try some code, catch any Exceptions
or throw an Exception
![Page 36: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/36.jpg)
36 / 89
Exception Handling
Keywords: try some code, catch any Exceptions
or throw an Exception
finally execute some code
![Page 37: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/37.jpg)
37 / 89
Exception Handling
Java forces you to deal with checked Exceptions
![Page 38: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/38.jpg)
38 / 89
Exception Handling
Java forces you to deal with checked Exceptions Two ways to deal with them:
void p1 () { try { riskyMethod(); } catch (Exception ex) { .... }
}
(a)
![Page 39: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/39.jpg)
39 / 89
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)
![Page 40: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/40.jpg)
40 / 89
Exception Handling
Remember the clone() method?
![Page 41: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/41.jpg)
41 / 89
Exception Handling
Remember the clone() method? Can be written in two ways:
Object clone() { try { return super.clone(); } catch (CloneNotSupportedException ex) { .... }} (a)
![Page 42: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/42.jpg)
42 / 89
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)
![Page 43: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/43.jpg)
43 / 89
Exception Handling
In the first case, we are catching and handling the Exception
![Page 44: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/44.jpg)
44 / 89
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
![Page 45: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/45.jpg)
45 / 89
Catching Exceptions A try-catch statement:
try {
// Statement(s) which throw Exceptions
} catch (Exception1 exception1) {
// Handles Exceptions of type Exception1
} catch (Exception2 exception2) {
// Handles Exceptions of type Exception2
} catch (Exception exception) {
// Handles Exceptions of type Exception
// ALL Exceptions
}
// Any code after the try-catch block
![Page 46: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/46.jpg)
46 / 89
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 (Exception exception) {
// Handles Exceptions of type Exception
// ALL Exceptions
}
// Any code after the try-catch block
![Page 47: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/47.jpg)
47 / 89
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 (Exception exception) {
// Handles Exceptions of type Exception
// ALL Exceptions
}
// Any code after the try-catch block
![Page 48: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/48.jpg)
48 / 89
Catching Exceptions
Thrown Exceptions have to be eventually caught somewhere in your code
![Page 49: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/49.jpg)
49 / 89
Exception Information
So an Exception has been caught – what can we do with it?
try {
// Statements which throw Exceptions
} catch (Exception exception) {
// ALL Exceptions
}
![Page 50: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/50.jpg)
50 / 89
Exception Information
Some useful methods in the Throwable class:
![Page 51: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/51.jpg)
51 / 89
Exception Information
Some useful methods in the Throwable class: String toString():
Returns a short description of the Exception
![Page 52: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/52.jpg)
52 / 89
Exception Information
Some useful methods in the Throwable class: String toString():
Returns a short description of the Exception
String getMessage(): Returns a detailed description of the Exception
![Page 53: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/53.jpg)
53 / 89
Exception Information
Some useful methods in the Throwable class: String toString():
Returns a short description of the Exception
String getMessage(): Returns a detailed description of the Exception
void printStackTrace(): Prints the stacktrace information on the console
![Page 54: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/54.jpg)
54 / 89
Exception Information
Some useful methods in the Throwable class:
void printStackTrace(): Prints the stacktrace information on the console
Sample output:
java.lang.NullPointerException at MyClass.method2(MyClass.java:9) at MyClass.method1(MyClass.java:6) at MyClass.main(MyClass.java:3)
![Page 55: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/55.jpg)
55 / 89
Exception Information Example:
java.io.PrintWriter output = null;
try {
output = new java.io.PrintWriter(“text.txt”);
output.println(“Welcome to Java”);
output.close();
}
catch (java.io.IOException ex){
System.out.println(ex.toString());
ex.printStackTrace() ;
}
![Page 56: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/56.jpg)
56 / 89
Problems Example:
java.io.PrintWriter output = null;
try {
output = new java.io.PrintWriter(“text.txt”);
output.println(“Welcome to Java”);
output.close();
}
catch (java.io.IOException ex){
System.out.println(ex.toString());
ex.printStackTrace() ;
}
Must execute output.close() even if Exception occurs
![Page 57: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/57.jpg)
57 / 89
Solution
Use the finally clause – for code that must be executed “no matter what”
![Page 58: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/58.jpg)
58 / 89
Solution
Use the finally clause – for code that must be executed “no matter what”
try {// Statement(s) which throw Exceptions
} catch (Exception1 exception1) {
// Handles Exceptions of type Exception1
} catch (Exception exception) {
// Handles Exceptions of type Exception
} finally {
// code executed whether there is an Exception or not
}
![Page 59: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/59.jpg)
59 / 89
Solution Example:
java.io.PrintWriter output = null;
try {
output = new java.io.PrintWriter(“text.txt”);
output.println(“Welcome to Java”);
}
catch (java.io.IOException ex){
ex.printStackTrace() ;
} finally {
if (output != null)
output.close();
}
![Page 60: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/60.jpg)
60 / 89
The finally block
Executed when try block is exited in these ways:
![Page 61: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/61.jpg)
61 / 89
The finally block
Executed when try block is exited in these ways: After last statement of try block
![Page 62: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/62.jpg)
62 / 89
The finally block
Executed when try block is exited in these ways: After last statement of try block
After last statement of the catch block (if an Exception was caught)
![Page 63: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/63.jpg)
63 / 89
The finally block
Executed when try block is exited in these ways: After last statement of try block
After last statement of the catch block (if an Exception was caught)
When an Exception is thrown in try but NOT caught
![Page 64: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/64.jpg)
64 / 89
The finally block
Executed when try block is exited in these ways: After last statement of try block
After last statement of the catch block (if an Exception was caught)
When an Exception is thrown in try but NOT caught
Executed even if there is a return statement prior to reaching the finally block
![Page 65: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/65.jpg)
65 / 89
Throwing Exceptions
If written code could encounter a runtime error:
![Page 66: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/66.jpg)
66 / 89
Throwing Exceptions
If written code could encounter a runtime error:
It creates an Exception object and throws it
![Page 67: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/67.jpg)
67 / 89
Throwing Exceptions
If written code could encounter a runtime error:
It creates an Exception object and throws it
and must also declare it in the method description
![Page 68: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/68.jpg)
68 / 89
Throwing Exceptions
If written code could encounter a runtime error:
It creates an Exception object and throws it
and must also declare it in the method description Only if the Exception is a checked Exception Optional for unchecked
![Page 69: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/69.jpg)
69 / 89
Throwing Exceptions
Example:
public void setRadius(double newRadius)
{
if (newRadius >= 0)
radius = newRadius;
else
throw new IllegalArgumentException(
"Radius cannot be negative");
}
![Page 70: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/70.jpg)
70 / 89
Throwing Exceptions
Example:OPTIONAL
public void setRadius(double newRadius) throws IllegalArgumentException
{
if (newRadius >= 0)
radius = newRadius;
else
throw new IllegalArgumentException(
"Radius cannot be negative");
}
![Page 71: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/71.jpg)
71 / 89
Throwing Exceptions
try {
Circle c1 = new Circle(5);
c1.setRadius(-5);
}
catch (IllegalArgumentException ex) { System.out.println(ex);
}
![Page 72: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/72.jpg)
72 / 89
Throwing Exceptions
try {
Circle c1 = new Circle(5);
c1.setRadius(-5);
}
catch (IllegalArgumentException ex) { System.out.println(ex);
}
Output:
Radius cannot be negative
![Page 73: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/73.jpg)
73 / 89
Creating Custom Exceptions
Create custom Exception classes if predefined classes not sufficient
![Page 74: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/74.jpg)
74 / 89
Creating Custom Exceptions
Create custom Exception classes if predefined classes not sufficient
To create a custom class: class should extend Exception
![Page 75: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/75.jpg)
75 / 89
Creating Custom Exceptions
Create custom Exception classes if predefined classes not sufficient
To create a custom class: class should extend Exception Good practice to add:
A default (empty) constructor A constructor with one String parameter
![Page 76: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/76.jpg)
76 / 89
Creating Custom Exceptions
public class InvalidRadiusException extends Exception {
private double radius;public InvalidRadiusException() {
super(“Invalid radius!”);
}
public InvalidRadiusException(double radius) {
super("Invalid radius!”);
this.radius = radius;
}
public double getRadius() {
return radius;
}
}
![Page 77: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/77.jpg)
77 / 89
Creating Custom Exceptions
Example:
public void setRadius(double newRadius) throws IllegalArgumentException
{
if (newRadius >= 0)
radius = newRadius;
else
throw new IllegalArgumentException(
"Radius cannot be negative");
}
![Page 78: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/78.jpg)
78 / 89
Creating Custom Exceptions
Example:
public void setRadius(double newRadius) throws InvalidRadiusException
{
if (newRadius >= 0)
radius = newRadius;
else
throw new InvalidRadiusException(radius);
}
![Page 79: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/79.jpg)
79 / 89
Creating Custom Exceptions
try {
Circle c1 = new Circle(5);
c1.setRadius(-5);
}
catch (IllegalArgumentException ex) { System.out.println(ex);
}
![Page 80: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/80.jpg)
80 / 89
Creating Custom Exceptions
try {
Circle c1 = new Circle(5);
c1.setRadius(-5);
}
catch (InvalidRadiusException ex) { System.out.println(“Invalid
Radius: “ + ex.getRadius());
}
![Page 81: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/81.jpg)
81 / 89
Creating Custom Exceptions
try {
Circle c1 = new Circle(5);
c1.setRadius(-5);
}
catch (InvalidRadiusException ex) { System.out.println(“Invalid
Radius: “ + ex.getRadius());
}
Output:
Invalid Radius: -5.0
![Page 82: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/82.jpg)
82 / 89
When to create Custom Exceptions
Use the exception classes in the API whenever possible.
![Page 83: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/83.jpg)
83 / 89
When to create Custom Exceptions
Use the exception classes in the API whenever possible.
Write your own custom exception class if you answer yes to one of the following:
![Page 84: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/84.jpg)
84 / 89
When to create Custom Exceptions
Use the exception classes in the API whenever possible.
Write your own custom exception class if you answer yes to one of the following: Do you need an exception type that isn’t represented by Java?
![Page 85: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/85.jpg)
85 / 89
When to create Custom Exceptions
Use the exception classes in the API whenever possible.
Write your own custom exception class if you answer yes to one of the following: Do you need an exception type that isn’t represented by Java?
Would it help users if they can differentiate your exceptions from those thrown by classes from other vendors?
![Page 86: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/86.jpg)
86 / 89
When to create Custom Exceptions
Use the exception classes in the API whenever possible.
Write your own custom exception class if you answer yes to one of the following: Do you need an exception type that isn’t represented by Java?
Would it help users if they can differentiate your exceptions from those thrown by classes from other vendors?
Do you want to pass more than just a string to the exception handler?
![Page 87: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/87.jpg)
87 / 89
When to use Exceptions
Use if the event is exception and truly an error
![Page 88: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/88.jpg)
88 / 89
When to use Exceptions
Use if the event is exception and truly an error Do not use it to deal with simple, expected
situations Example:
try { System.out.println(refVar.toString());}catch (NullPointerException ex) { System.out.println("refVar is null");}
![Page 89: 1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.](https://reader036.fdocuments.us/reader036/viewer/2022062518/56649cba5503460f94981c57/html5/thumbnails/89.jpg)
89 / 89
When to use Exceptions
Use if the event is exception and truly an error Do not use it to deal with simple, expected
situations Example:
Replace with:
try { System.out.println(refVar.toString());}catch (NullPointerException ex) { System.out.println("refVar is null");}
if (refVar != null) System.out.println(refVar.toString());else System.out.println("refVar is null");