-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExcep_handle.java
More file actions
33 lines (25 loc) · 766 Bytes
/
Excep_handle.java
File metadata and controls
33 lines (25 loc) · 766 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
/*
Write a Java program to read two integers a andb. Compute a/b and print, when b is not
zero. Raise an exception when b is equal to zero.
*/
import java.io.*;
import java.util.*;
class Excep_handle
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter A: ");
int a = s.nextInt();
System.out.println("Enter B: ");
int b = s.nextInt();
try{
float res=a/b;//This will raise an exception if denominator is 0 hence we place it inside try{}
System.out.println("Result is "+res);//If there is not exception the res is printed
}
catch(Exception e)
{ //If an exception occurs then we catch and show the exception
System.out.println("Exception Caught!! \n "+e.getMessage());
}
}
}