-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathExcep.java
More file actions
40 lines (38 loc) · 951 Bytes
/
Excep.java
File metadata and controls
40 lines (38 loc) · 951 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
//Write a Java program that shows the usage of try, catch, throws and finally.
import java.util.Scanner;
class Excep
{
public static void divide(int a, int b) throws ArithmeticException
{
if(b==0)
{
throw new ArithmeticException("Divide by Zero not possible");
}
else
{
System.out.println("Result ="+a/b);
}
}
public static void main(String args[])
{
int x,y;
try
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of X & Y");
x= sc.nextInt();
sc.nextLine();
y= sc.nextInt();
sc.nextLine();
divide(x, y);
}
catch(ArithmeticException e)
{
System.out.println(e.getMessage());
}
finally
{
System.out.println("End of the Program");
}
}
}