Object-Oriented Programming: Exceptions
Object-Oriented Programming: Exceptions
Object-Oriented Programming: Exceptions
Exceptions
Handling exceptional events
Acknowledgement
The contents of these slides have origin from
School of Computing, National University of
Singapore.
We greatly appreciate support from Mr. Aaron
Tan Tuck Choy, and Dr. Low Kok Lim for
kindly sharing these materials.
2
Policies for students
These contents are only used for students
PERSONALLY.
Students are NOT allowed to modify or
deliver these contents to anywhere or anyone
for any purpose.
3
Objectives
Understand how to use the mechanism of
exceptions to handle errors or exceptional
events that occur during program execution
Book
• Chapter 1, Section 1.6, pages
64 to 72
throw ExceptionObject;
Constructor
ExceptionClassName(String Msg)
Construct an exception object with the error message Msg
void printStackTrace()
Print the calling stack
Note:
A method can throw more than one type of exception
[503005 Lecture 6: Exceptions]
15
3. Exception Handling: Syntax
As the user of a method that can throw exception(s):
It is your responsibility to handle the exception(s)
Also known as exception catching
try { // try block
statement(s); // exceptions might be thrown
} // followed by one or more catch block
catch (ExpClass1 obj1) { // a catch block
statement(s); // Do something about the exception
} // catch block for another type of
catch (ExpClass2 obj2) { exception
statement(s);
}
finally { // finally block – for cleanup code
statement(s);
}
try {
System.out.println("Ans = " + factorial(input));
}
catch (IllegalArgumentException expObj) {
System.out.println(expObj.getMessage());
}
We choose to print out the error message in this
}
} case. There are other ways to handle this error.
See next slide for more complete code.
[503005 Lecture 6: Exceptions]
17
4. Execution Flow (1/2)
TestException.java
public static int factorial(int n)
throws IllegalArgumentException {
System.out.println("Before Checking"); Enter n: 4
if (n < 0) { Before factorial()
throw new IllegalArgumentException(...); Before Checking
} After Checking
System.out.println("After Checking");
Ans = 24
//... other code not shown
} After factorial()
Finally!
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter n: ");
int input = sc.nextInt();
try {
System.out.println("Before factorial()");
System.out.println("Ans = " + factorial(input));
System.out.println("After factorial()"); Enter n: -2
} catch (IllegalArgumentException expObj) { Before factorial()
System.out.println("In Catch Block");
System.out.println(expObj.getMessage());
Before Checking
} finally { In Catch Block
System.out.println("Finally!"); -2 is invalid!
} Finally!
}
[503005 Lecture 6: Exceptions]
18
4. Execution Flow (2/2)
Another version
Keep retrying if n < 0
TestExceptionRetry.java
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); Enter n: -2
int input; -2 is invalid!
boolean retry = true; Enter n: -7
do { -7 is invalid!
try { Enter n: 6
System.out.print("Enter n: "); Ans = 720
input = sc.nextInt();
System.out.println("Ans = " + factorial(input));
retry = false; // no need to retry
} catch (IllegalArgumentException expObj) {
System.out.println(expObj.getMessage());
}
} while (retry);
}
try {
...
} catch (MyException e) {
...
}
[503005 Lecture 6: Exceptions]
22
5. Example: Bank Account (1/5)
public class NotEnoughFundException extends Exception {
public BankAcct() {
// By default, numeric attributes are initialised to 0
}
} // class BankAcct
System.out.println("Depositing $200...");
acc.deposit(200.0);