-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomException.java
More file actions
33 lines (33 loc) · 863 Bytes
/
CustomException.java
File metadata and controls
33 lines (33 loc) · 863 Bytes
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
/*
* We can create custom exception with the help of CustomException
*
*/
class AgeLimit extends Exception{
@Override
public String toString(){
return "Age is to0 small";
}
@Override
public String getMessage(){
return "Age is to0 small";
}
}
public class CustomException {
public static void main(String[] args) {
int age;
java.util.Scanner sc=new java.util.Scanner(System.in);
System.out.print("Enter your age = ");
age=sc.nextInt();
if(age<18){
try{
throw new AgeLimit();
}
catch(AgeLimit al)
{
System.out.println(al.getMessage());
al.printStackTrace(); //tells us which error happened at which line
}
}
System.out.println("Thank you!!!!");
}
}