-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionChainingExample.java
More file actions
63 lines (55 loc) · 1.63 KB
/
Copy pathExceptionChainingExample.java
File metadata and controls
63 lines (55 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package java1_4.exceptions;
import java.io.IOException;
/**
* Java 1.4 Exception Chaining Example Demonstrates exception chaining
*/
public class ExceptionChainingExample
{
public static void main(String[] args)
{
System.out.println("=== Java 1.4 Exception Chaining ===\n");
try
{
processFile("nonexistent.txt");
}
catch (ProcessingException e)
{
System.out.println("Caught: " + e.getMessage());
System.out.println("Original cause: " + e.getCause().getClass().getSimpleName());
System.out.println("Cause message: " + e.getCause().getMessage());
System.out.println("\nFull stack trace:");
e.printStackTrace();
}
System.out.println("\nKey Features:");
System.out.println("- Preserve exception history");
System.out.println("- Better error diagnostics");
System.out.println("- Cause tracking");
System.out.println("- Improved debugging");
System.out.println("\nBenefits:");
System.out.println("- Maintains full exception chain");
System.out.println("- Better error reporting");
System.out.println("- Easier debugging");
System.out.println("- Preserves original exception context");
}
private static void processFile(String filename) throws ProcessingException
{
try
{
// Simulate file operation that throws IOException
throw new IOException("File not found: " + filename);
}
catch (IOException e)
{
// Chain the exception
throw new ProcessingException("Failed to process file", e);
}
}
}
// Custom exception with chaining
class ProcessingException extends Exception
{
public ProcessingException(String message, Throwable cause)
{
super(message, cause); // Chain the exception
}
}