-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserDefineExpection.java
72 lines (67 loc) · 1.86 KB
/
UserDefineExpection.java
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import java.util.Scanner;
class HrsException extends Exception {
private static final long serialVersionUID = 1L;
HrsException(String s) {
super(s);
}
}
class MinException extends Exception {
private static final long serialVersionUID = 1L;
MinException(String s) {
super(s);
}
}
class SecException extends Exception {
private static final long serialVersionUID = 1L;
SecException(String s) {
super(s);
}
}
class Time {
int hrs, min, sec;
Time(int h, int m, int s) {
hrs = h;
min = m;
sec = s;
}
void display() throws HrsException,MinException,SecException {
if (hrs < 0 || hrs > 24)
throw new HrsException("not valid");
else
System.out.print(hrs + " hr ");
if (min < 0 || min > 60)
throw new MinException("not valid");
else
System.out.print(min + " min ");
if (sec < 0 || sec > 60)
throw new SecException("not vaild");
else
System.out.print(sec + " sec");
}
}
public class UserDefineExpection {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Hours ");
int hours = sc.nextInt();
System.out.println("Min ");
int minutes = sc.nextInt();
System.out.println("sec ");
int seconds = sc.nextInt();
sc.close();
Time mytime = new Time(hours,minutes,seconds);
try{
System.out.println("Time is: ");
mytime.display();
}
catch(HrsException e){
System.out.println(e);
}
catch(MinException e){
System.out.println(e);
}
catch(SecException e){
System.out.println(e);
}
}
}