Learning Unit 5: Exception Handling
What is an Exception?
- An exception is an error that occurs during runtime, disrupting the normal program flow.
- Types of Exceptions:
- Programming Errors:
- Divide by zero error: Attempting to divide a number by zero.
- Array Access Violation: Accessing elements beyond the array's defined range.
- Normal Program Errors:
- Invalid Input: User provides input that the program cannot process.
- File Access Issues: Attempting to open a file that doesn’t exist.
- System Errors:
- Out of Memory Errors: Indicates that the heap memory is exhausted.
Exception Classes and Hierarchy
- The exception handling in Java is organized in a hierarchy:
- Throwable (root class for exceptions)
- Error (issues beyond user's control)
- Exception (issues caused by user programs)
- Common Exceptions include:
IOException, ArithmeticException, NullPointerException and many others.
Example: Exception Causing Code
class DivByZero {
public static void main(String args[]) {
// This will cause an exception
System.out.println(3/0);
System.out.println("I don't get to print..");
}
}
Default Exception Handler in Java
- When an exception occurs, Java's Runtime Environment handles it by:
- Displaying an error message and the stack trace (the chain of method calls that led to the exception).
- Terminating the program if the exception is not caught.
Exception Detection Process
- An exception is detected when an exceptional condition occurs, creating an Exception object which contains:
- Information about the error and the program state at the time of the error.
- Throwing an Exception: Passing the exception object to the runtime is called throwing an exception.
Searching the Call Stack
- When an exception occurs, the Java runtime searches the call stack for a suitable exception handler.
- If it finds an appropriate handler, it passes the exception to it. If not, the default handler is invoked, which terminates the program.
Benefits of Exception Handling Framework
- Separation of Error-Handling Code:
- Cleaner and more readable code as error-handling logic is decoupled from business logic.
- Proper Error Handling by Suitable Handlers:
- Errors propagate up the call stack until a suitable handler is found.
- Grouping Errors by Types:
- Exception classes are structured in a hierarchy for better organization and handling.
Catching Exceptions
- Using try-catch Statements:
- Syntax:
java
try {
<code that might throw an exception>
} catch (ExceptionType e) {
<handler code>
}
- Example:
java
class DivByZero {
public static void main(String args[]) {
try {
System.out.println(3/0);
} catch (ArithmeticException exc) {
System.out.println(exc);
}
System.out.println("After exception.");
}
}
Catching Exceptions with finally
- Using finally Keyword:
- This block executes regardless of whether an exception occurs or not. It's typically used for cleanup operations.
- Syntax:
java
try {
// code that may cause an exception
} catch (ExceptionType e) {
// handle exception
} finally {
// cleanup code
}
Throwing Exceptions
- Java allows programmers to throw exceptions manually using the
throw keyword:- `throw new ExceptionType(