WEEK 7-8: EXCEPTION HANDLING Flashcards
Unexpected or error conditions
An abnormal condition that must be properly handled to prevent program termination
Exceptions
Causes of Exceptions
The program issues a call to a file that does not exist
The program attempts to write to a full disk
The user enters invalid data
The program attempts to divide a value by 0
Object-oriented techniques used to manage Exception errors
Runtime exceptions
Exception handling
Represents serious errors from which a program usually cannot recover
Error condition
A program runs out of memory
A program cannot locate a required class
Error class
Less serious errors
Unusual conditions
A program can recover from this type of error
Exception class
A segment of code in which something might go wrong
Attempts to execute
Acknowledges an exception might occur
Try block
A try block includes:
The keyword try
Opening and closing curly braces
Executable statements, which might cause an exception
A segment of code that immediately follows a try block
Handles an exception thrown by the try block preceding it
Can “catch” an Object of type Exception or an Exception child class
catch block
A catch block includes:
The keyword catch
Opening and closing parentheses
An Exception type
A name for an instance of the Exception type
Opening and closing curly braces
Statements to handle the error condition
Use for actions you must perform at the end of a try catch sequence
Use to perform cleanup tasks
Executes regardless of whether the preceding try block identifies an exception
finally block
When the try code fails:
It throws an exception
The Exception object is caught
The catch block executes
Reasons the final set of statements might never execute:
An unplanned exception might occur
The try or catch block might contain a System.exit(); statement
If the try block might throw an Exception object for which you did not provide a catch block:
Program execution stops immediately
The exception is sent to the operating system for handling
The current method is abandoned