-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLab9.java
More file actions
40 lines (35 loc) · 1016 Bytes
/
Lab9.java
File metadata and controls
40 lines (35 loc) · 1016 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
39
40
// 9: Develop a JAVA program to raise a custom exception (user defined exception) for DivisionByZero using try, catch, throw and finally
import java.util.Scanner;
class IException extends Exception {
IException(String msg) {
super(msg);
}
}
public class Lab9 {
static void divide(double a, double b) throws IException {
if (b == 0) {
throw new IException("Division by zero is not allowed");
}
double c = a / b;
System.out.println("Result= " + c);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter two numbers: ");
double a = sc.nextDouble();
double b = sc.nextDouble();
try {
divide(a, b);
} catch (IException e) {
System.out.println(e);
} finally {
System.out.println("End");
}
}
}
/*
* Output:
* Enter two numbers: 10 0
* IException: Division by zero is not allowed
* End
*/