-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathException133.java
More file actions
51 lines (41 loc) · 1.39 KB
/
Exception133.java
File metadata and controls
51 lines (41 loc) · 1.39 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
47
48
49
50
51
// Writing my own exception class
class InvalidBoxDimException extends RuntimeException{
InvalidBoxDimException(double invalidDim){
System.out.println("Box instance with invalid Dimension "+invalidDim);
}
}
// Creating a box class for calculating area and volume
class Box12{
private double len;
private double wid;
private double hei;
Box12(double len, double wid, double hei) throws InvalidBoxDimException{
if(len<=0) throw new InvalidBoxDimException(len);
if(wid<=0) throw new InvalidBoxDimException(wid);
if(hei<=0) throw new InvalidBoxDimException(hei);
this.len=len; this.wid=wid; this.hei=hei;
}
public double area(){
return 2*(len*wid + wid*hei + hei*len);
}
public double volume(){
return len*wid*hei;
}
}
// Driver code // or Client code where we will throw exception from
public class Exception133 {
public static void main(String[] args) {
try{
Box12 b1 = new Box12(10,0,56);
System.out.println(b1.area());
System.out.println(b1.volume());
}catch (InvalidBoxDimException e){
}
try{
Box12 b2 = new Box12(10,0,56);
System.out.println(b2.area());
System.out.println(b2.volume());
}catch (InvalidBoxDimException e){
}
}
}