In the print dialog, choose "Save as PDF" as the destination.
Error types, debugging technique, and handling exceptions gracefully
Java Errors
Compile-Time
Detected before the program runs: it won't even compile (missing semicolon, undeclared variable)
Runtime
Occur while the program is running and crash it (division by zero, array index out of bounds)
Logical
The program runs and produces a result, but the result is wrong (hardest to find)
int x = 5 // Error: ';' expected , fix: add a semicolon
System.out.println(myVar); // Error: cannot find symbol
int myVar = 50; // fix: declare before use
int x = "Hello"; // Error: incompatible types
String x = "Hello"; // fix: use the correct typeint x = 10, y = 0;
int result = x / y; // ArithmeticException: / by zero
int[] numbers = {1, 2, 3};
System.out.println(numbers[10]); // ArrayIndexOutOfBoundsException: 10int x = 10, y = 2;
int result = x - y; // meant x + y
System.out.println(result); // prints 8, not 12System.out.println() calls to trace state before the failure pointCode has bug
Add print statements
Run program
Check output
Found problem?
Fix bug
Test again
System.out.println("x = " + x);
System.out.println("y = " + y);
int result = x / y; // crash happens here , trace shows exactly what led to ittry-catch lets you handle it gracefully insteadExecute try block
Error occurs?
Skip catch block
Execute catch block
Continue program
try {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
} catch (Exception e) {
System.out.println("Something went wrong.");
}
// Output: Something went wrong. (program continues)finally runs always, whether an exception occurred or nottry block
Error?
catch block
Skip catch
finally block
Continue
try {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
} catch (Exception e) {
System.out.println("Something went wrong.");
} finally {
System.out.println("The 'try catch' is finished.");
}throwstatic void checkAge(int age) {
if (age < 18) {
throw new ArithmeticException("Access denied - must be 18+.");
}
System.out.println("Access granted!");
}
try {
checkAge(15);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
// Output: Error: Access denied - must be 18+.finally block that closes the resource, verbose and error-pronetry (FileWriter fw = new FileWriter("output.txt");
BufferedWriter bw = new BufferedWriter(fw)) {
bw.write("Hello World!");
} catch (Exception e) {
e.printStackTrace();
}
// Both resources close automatically, in reverse orderOpen Resource 1
Open Resource 2
Use Resources
Close Resource 2
Close Resource 1
| Exception | Cause |
|---|---|
ArithmeticException | Division by zero |
ArrayIndexOutOfBoundsException | Invalid array index |
NullPointerException | Using a null object |
FileNotFoundException | File doesn't exist |
NumberFormatException | Invalid string-to-number conversion |
try-catch prevents crashes by handling exceptions gracefully; finally always runs for cleanupthrow lets you raise your own exceptions with meaningful messagesJava Exceptions and Debugging
Error Types
Compile-Time, Runtime, Logical
Debugging
Print statements, IDE debugger, check variables
Exception Handling
try-catch, finally, throw
Best Practices
try-with-resources, specific exceptions, meaningful messages
Mahmoud Abas|Java Exceptions and Debugging Lab