Skip to content

Commit 56ba64a

Browse files
authored
Create ChainedException.java
1 parent 617cc8e commit 56ba64a

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Diff for: ChainedException.java

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Java program to demonstrate working of chained exceptions
2+
public class ChainedException
3+
{
4+
public static void main(String[] args)
5+
{
6+
try
7+
{
8+
// Creating an exception
9+
NumberFormatException ex =
10+
new NumberFormatException("Exception");
11+
12+
// Setting a cause of the exception
13+
ex.initCause(new NullPointerException(
14+
"This is actual cause of the exception"));
15+
16+
// Throwing an exception with cause.
17+
throw ex;
18+
}
19+
20+
catch(NumberFormatException ex)
21+
{
22+
// displaying the exception
23+
System.out.println(ex);
24+
25+
// Getting the actual cause of the exception
26+
System.out.println(ex.getCause());
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)