-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathThrow2.java
36 lines (35 loc) · 945 Bytes
/
Throw2.java
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
package Exception_Handling;
// To continue execution of code after using the throw keyword.
public class Throw2
{
static void validate(int age)
{
if(age<18)
{
throw new ArithmeticException("not valid");
}
else
{
System.out.println("welcome to vote");
}
}
public static void main(String args[])
{
try
{
try
{
validate(13);
}
catch(NullPointerException e)
{
System.out.println("Thrown error has been caught: "+e); // Since we caught the wrong exception, line wont be executed.
}
}
catch(ArithmeticException e)
{
System.out.println("Thrown error has been caught now: "+e ); // Since correct exception handling, line is executed.
}
System.out.println("rest of the code...");
}
}