Skip to content

Commit 46f77ef

Browse files
committed
Exception Handling
1 parent 3804ce0 commit 46f77ef

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

ExceptionHandling.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.util.*;
2+
class ExceptionHandling{
3+
public static void main(String[] args){
4+
Scanner sc = new Scanner(System.in);
5+
int a=0,b,c;
6+
try{
7+
System.out.println("Enter a = ");
8+
a = sc.nextInt();
9+
System.out.println("Enter b = ");
10+
b = sc.nextInt();
11+
c = a / b;
12+
System.out.println(c);
13+
}catch(ArithmeticException e){
14+
System.out.println(e);
15+
}
16+
finally{
17+
System.out.println("I am finally and i always run...");
18+
}
19+
System.out.println("program is still working...");
20+
21+
}
22+
}

ExceptionHandling2.java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.util.*;
2+
class AgeException extends Exception{
3+
public AgeException(String msg){
4+
super(msg);
5+
}
6+
}
7+
class ExceptionHandling2{
8+
private static void verifyAge(int age) throws AgeException{
9+
if(age<18){
10+
throw new AgeException("You can not vote");
11+
}else{
12+
System.out.println("you can vote");
13+
}
14+
}
15+
public static void main(String[] args){
16+
Scanner sc = new Scanner(System.in);
17+
System.out.println("Enter your age : ");
18+
int age = sc.nextInt();
19+
try{
20+
verifyAge(age);
21+
}catch(AgeException e){
22+
System.out.println(e);
23+
}
24+
finally{
25+
System.out.println("I am finally and i always run...");
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)