-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMexception.java
More file actions
46 lines (45 loc) · 1.46 KB
/
Copy pathCMexception.java
File metadata and controls
46 lines (45 loc) · 1.46 KB
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
39
40
41
42
43
44
45
46
/*What is a custom exception in Java, and why might you use one? Write a Java class
InvalidAgeException that extends the Exception class. This exception should be
thrown when an invalid age (less than 0 or greater than 150) is provided. Demonstrate
how to use this custom exception in a method that checks the validity of an age.*/
import java.util.Scanner;
import java.lang.*;
class InvalidAgeException extends Exception {
InvalidAgeException(String s){
super(s);
}
}
class CMException{
String name;
double age;
CMException(){
name="";
age=0;
}
CMException(String name,double age){
this.name=name;
this.age=age;
}
void display(){
System.out.println("Name is "+this.name);
System.out.println("Age is "+this.age);
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("please enter name");
String name=sc.nextLine();
System.out.println("please enter age");
double age=sc.nextDouble();
CMException C1=new CMException(name,age);
try{
if(C1.age<0 || C1.age>150){
throw new InvalidAgeException("Invalid age is provided");
}
else{
C1.display();
}
}catch(java.lang.Exception e){
System.out.println(e.getMessage());
}
}//ens main
}//end class