Exceptions1
Exceptions1
Java
• Java has a predefined set of exceptions and errors that can occur during
execution
10-4
Errors and Error Handling
An Error is any unexpected result obtained from a
program during execution.
Unhandled errors may manifest themselves as
incorrect results or behavior, or as abnormal
program termination.
Errors should be handled by the programmer, to
prevent them from reaching the user.
• A try statement can have an optional clause following the catch clauses,
designated by the reserved word finally
10-25
Code Examples
1. Demonstration of an unchecked exception (NullPointerException)
2. Demonstration of checked exceptions:
Passing a DivideByZeroException
Handling a DivideByZeroException
• The parent class used depends on how the new exception will be used
10-28
Checked Exceptions - Summary
10-29
Unchecked Exceptions- Summary
• An unchecked exception does not require explicit handling, though it
could be processed that way
10-30
I/O Exceptions
10-31
Standard I/O
10-32
The IOException Class
10-33
Writing Text Files
• The FileWriter class represents a text output file, but with minimal
support for manipulating data
10-34