1. What are Exceptions and Why Handle Them?¶
Imagine you write a Java program to calculate the quotient of two numbers. If the user enters a divisor of 0, the program suddenly pops up an error window and stops running—that’s when the program encounters an “exception.”
Exceptions are like “small accidents” during program execution, such as sudden power outages, missing files, or network disconnections. If unhandled, these exceptions can cause the program to crash, resulting in a very poor user experience. By using try-catch to capture exceptions, the program can “handle these accidents calmly” and continue running stably.
2. Basic Syntax and Execution Flow of try-catch¶
The core role of try-catch is: Put code that might go wrong in the try block, and if an error occurs, the catch block will capture and handle the error.
Syntax Structure:¶
try {
// Code that may throw an exception (e.g., division, array access)
} catch (ExceptionType variableName) {
// Code to execute when an exception occurs (usually printing error info)
}
Execution Flow:¶
- If there is no exception in the
tryblock, the code in thecatchblock will not execute, and the program will continue running after thetry-catchblock. - If an exception occurs in the
tryblock, the code after the exception in thetryblock will not execute. The program will immediately jump to the correspondingcatchblock, execute it, and then continue running afterward.
3. Practical: Handling Common Exceptions with try-catch¶
Example 1: Handling “ArithmeticException” (Division by Zero)¶
Let’s write a simple division program:
public class TryCatchDemo {
public static void main(String[] args) {
int a = 10;
int b = 0; // Intentionally set to 0 to trigger division by zero
try {
int result = a / b; // This will throw ArithmeticException
System.out.println("Result: " + result); // This line won't execute
} catch (ArithmeticException e) {
// Execute after catching the exception: print error message
System.out.println("Error: Divisor cannot be zero!");
}
// After exception handling, the program continues running
System.out.println("Program did not crash; continue executing subsequent code!");
}
}
Output:
Error: Divisor cannot be zero!
Program did not crash; continue executing subsequent code!
Key Point: After the a/b in the try block throws an exception, the catch block catches ArithmeticException, executes the error message, and the subsequent System.out.println still runs normally.
Example 2: Handling “NullPointerException” (Null Pointer)¶
When you accidentally set an object to null and call a method on it, a null pointer exception is thrown:
public class NullExceptionDemo {
public static void main(String[] args) {
String str = null; // The string variable str is set to null
try {
str.length(); // Calling the length() method on a null object throws NullPointerException
System.out.println("String length: " + str.length()); // This line won't execute
} catch (NullPointerException e) {
System.out.println("Error: str is a null object; cannot call length() method!");
}
System.out.println("Program continues running!");
}
}
Output:
Error: str is a null object; cannot call length() method!
Program continues running!
4. Common Exception Types and Solutions¶
1. ArithmeticException (Division by Zero)¶
- Scenario:
int result = 10 / 0; - Solution: Ensure the divisor is not zero, or use
try-catchto handle it.
2. ArrayIndexOutOfBoundsException (Array Index Out of Bounds)¶
- Scenario:
int[] arr = {1, 2, 3}; arr[3] = 5;(The array index is 0-based, so max index is 2) - Solution: Check if the array index is valid (less than the array length).
3. NullPointerException (Null Pointer)¶
- Scenario:
Object obj = null; obj.method();(Calling a method on a null object) - Solution: Ensure the object is not null, or use
try-catchto handle it.
5. finally Block: Code to Execute Regardless of Exception¶
The finally block is optional but extremely important! The code in the finally block will execute no matter whether the try block throws an exception or not. It is commonly used to release resources (e.g., closing files, database connections).
Example:
try {
// Attempt to open a file/database connection (may fail)
System.out.println("Attempting to open resources...");
} catch (Exception e) {
System.out.println("Failed to open resources!");
} finally {
// Close resources regardless of success/failure
System.out.println("Closing resources... executed whether there was an error or not!");
}
6. Best Practices for Exception Handling¶
-
Catch Specific Exceptions: Avoid catching only
Exception(the parent class), and instead catch specific exception types (e.g.,ArithmeticException). This prevents “over-catching.” -
Do Not Ignore Exceptions: At least print the error message (e.g.,
e.printStackTrace()). For debugging:
catch (Exception e) {
e.printStackTrace(); // Print exception stack trace to locate issues
}
- Use finally Reasonably: Ensure resources (files, network connections, etc.) are properly closed to avoid resource leaks.
Summary¶
try-catch is the core tool for handling exceptions in Java, allowing the program to remain stable even when encountering “accidents.” Remember: Exceptions are like small surprises in life—with proper preparation (try-catch), your program becomes more robust and reliable.
After learning this, you should be able to start adding try-catch to your code to make your programs “smart” about handling errors!