-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustom_Exception.java
More file actions
62 lines (58 loc) · 2.42 KB
/
Custom_Exception.java
File metadata and controls
62 lines (58 loc) · 2.42 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
52
53
54
55
56
57
58
59
60
61
62
import java.util.InputMismatchException;
import java.util.Scanner;
public class NumberFormatException_Handling {
String stu_name;
int marks_sub1;
int marks_sub2;
int marks_sub3;
NumberFormatException_Handling(String stu_name, int marks_sub1, int marks_sub2, int marks_sub3) {
this.stu_name = stu_name;
this.marks_sub1 = marks_sub1;
this.marks_sub2 = marks_sub2;
this.marks_sub3 = marks_sub3;
}
NumberFormatException_Handling(){
super();
}
Scanner s = new Scanner(System.in);
public void error() throws NegativeValueException, ValuesOutOfRangeException {
NumberFormatException_Handling obj1 = new NumberFormatException_Handling(s.next(), s.nextInt(), s.nextInt(), s.nextInt());
NumberFormatException_Handling obj2 = new NumberFormatException_Handling(s.next(), s.nextInt(), s.nextInt(), s.nextInt());
if (obj1.marks_sub1 < 0 || obj1.marks_sub2 < 0 || obj1.marks_sub3 < 0 || obj2.marks_sub1 < 0 ||
obj2.marks_sub2 < 0 || obj2.marks_sub3 < 0) {
throw new NegativeValueException("Input Is Negative (invalid)");
}
else if(obj1.marks_sub1 > 100 || obj1.marks_sub2 > 100 || obj1.marks_sub3 > 100
|| obj2.marks_sub1 > 100 || obj2.marks_sub2 > 100 || obj2.marks_sub3 > 100){
throw new ValuesOutOfRangeException("Input is out of Range");
}
else {
double avg1 = (obj1.marks_sub1 + obj1.marks_sub2 + obj1.marks_sub3) / 3;
double avg2 = (obj2.marks_sub1 + obj2.marks_sub2 + obj2.marks_sub3) / 3;
System.out.println("Average marks of Student 1 :: " + avg1);
System.out.println("Average marks of Student 2 :: " + avg2);
}
}
public void duplicate(){
try{
error();
}
catch (InputMismatchException | NegativeValueException | ValuesOutOfRangeException ex){
System.out.println("Exception occurred :: " +ex);
}
}
public static void main(String[] args) {
NumberFormatException_Handling obj = new NumberFormatException_Handling();
obj.duplicate();
}
}
class NegativeValueException extends Exception{
public NegativeValueException(String exp1){
super(exp1);
}
}
class ValuesOutOfRangeException extends Exception{
public ValuesOutOfRangeException(String exp2){
super(exp2);
}
}