COMP 202 Lecture Notes - Lecture 9: Exception Handling

32 views6 pages
COMP 202: Foundations of Programming
Lecture 9: Exceptions cont. and Object-Oriented Programming
Flow of try-catch-finally:
Catching Exceptions:
A catch clause cannot exist without a try statement. It is not compulsory to have finally clauses whenever a
try/catch block is present. However, the try block cannot be present without either a catch clause or a finally
clause.
As well as this, there can be no other code between the try, catch, and finally blocks.
Mandatory Catching:
Broadly speaking, there are two kinds of Exceptions checked, and unchecked.
The past exceptions we have seen in class so far have been unchecked (and give us an error statement). These
can cause your code to crash at runtime.
Checked Exceptions:
Checked exceptions will not cause your code to crash. If you are using a method that could throw a
checked exception, you will get a compiler error if you do not take precautions to handle the exception.
Usually, these kinds of exceptions happen due to something that isn’t the programmer’s fault.
E.g. trying to read from a file whose name is misspelled.
o These are considered “recoverable” errors must be fixed before continuing
Examples of Checked vs. Unchecked Exceptions:
Checked:
- Exception
- IOException
Unchecked:
- NullPointerException
- Arithmetic Exception
- ArrayIndexOutOfBoundsException
Since all different exceptions are a “type” of Exception, Exception is checked.
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 6 pages and 3 million more documents.

Already have an account? Log in
COMP 202: Foundations of Programming
Handling Checked Exceptions:
There are two main ways to handle a checked exception.
1. Surround it with a try block
2. Declare in the method header that it throws an Exception
So when the method is called, the exception will have to be handled using a try block.
Exception Propagation:
If an exception is not caught in the current method, it will then go up to the method that called it, and can be
handled by a try-catch block there.
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 6 pages and 3 million more documents.

Already have an account? Log in
jc123 and 40170 others unlocked
COMP 202 Full Course Notes
100
COMP 202 Full Course Notes
Verified Note
100 documents

Document Summary

A catch clause cannot exist without a try statement. It is not compulsory to have finally clauses whenever a try/catch block is present. However, the try block cannot be present without either a catch clause or a finally clause. As well as this, there can be no other code between the try, catch, and finally blocks. Broadly speaking, there are two kinds of exceptions checked, and unchecked. The past exceptions we have seen in class so far have been unchecked (and give us an error statement). These can cause your code to crash at runtime. Checked exceptions will not cause your code to crash. If you are using a method that could throw a checked exception, you will get a compiler error if you do not take precautions to handle the exception. Usually, these kinds of exceptions happen due to something that isn"t the programmer"s fault.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents