Lecture 15:
Exception Handling
COMP102: Computer Programming
September 2024
Einstein’s Definition of Insanity
“The definition of insanity is --- doing the same
thing over and over and expecting a different
result.”
Importance of Practice in Learning Skill
[1] How to use tools
[2] How to do stuff (cut
pieces, join pieces)
[3] Knowledge – strong sturdy
structures
[4] Abstract thinking for creating
and implementing design
[Link] (Accessed 16
September 2024)
Don’t Underestimate Task at Hand
[1] Mastery required for
transfer to new problem
[2] Mastery fosters confidence
(seen this problem/solved a
similar problem)
[3] Mastery allows you to see
patterns in problem and recall
building blocks of solution
How to Think About Test 1 Result
1. Result is a judgement of my intelligence and
ability (wrong!)
2. Reflection of my current understanding and
mastery (right!)
➢
Current understanding and mastery, given: time + effort
I’ve put into the course
Still Time for Turnaround
Assessment Weight Date
Practical’s 5% Friday afternoon
Test 1 15% 30th August 2024
Test 2 10% 4 October 2024
Practical Test 20% 11 October 2024
Exam 50% 14th November 2024
[Plan to Pass] Course Calendar
Week Work
[1] 30 September Test 2 [Friday]
File processing
[2] 7 October Testing
[3] 14 October Practical Test [Friday]
[4] 21 October Make-up Tests
Exceptions
Consider Following Program
Output: Expected
Output: Bad
What’s an Exception
- “An exception is an abnormal condition that arises in a code sequence at run
time.” – Java: The Complete Reference (11 ed.) – Herbert Schildt (Danny
Coward, technical editor)
- Three types of errors:
- Syntax or compile-time errors
- Logical errors
- Runtime errors
- Sources of exceptions:
- Java Virtual Machine
- Your source code
Causes of Exceptions
Java’s Error Handling Mechanism
- Has four components:
- Exception classes
- throws clause
- try-catch statement
- throw statement
try-catch Statement
try {
// Risky code which could cause an exception to be thrown
} catch (Exception e) {
// What must happen if an exception is thrown
} finally {
// Executed whether or not exception is thrown
}
New Code
Output: Errors Handled Gracefully
Ends.