-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgeDemo.java
More file actions
38 lines (38 loc) · 815 Bytes
/
Copy pathAgeDemo.java
File metadata and controls
38 lines (38 loc) · 815 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
34
35
36
37
38
import java.util.Scanner;
import java.lang.Exception;
class AgeExceptionEx extends Exception{
public AgeExceptionEx(){
super();
}
public AgeExceptionEx(String msg){
super(msg);
}
}
class AgeDemo{
int age;
AgeDemo(){
age=0;
}
AgeDemo(int age) throws AgeExceptionEx{
if(age<0 ||age>120){
throw new AgeExceptionEx("Invalid input please enter value in range 0 to 120");
}
this.age=age;
}
void display(){
System.out.println("age is "+this.age);
}
public static void main(String[] args){
Scanner sc= new Scanner(System.in);
sc.nextLine();
try{
System.out.println("please enter age");
int age=sc.nextInt();
AgeDemo A=new AgeDemo(age);
A.display();
}
catch(Exception e){
System.out.println("Error found "+e.getMessage());
}
}
}