-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtry_catch.java
27 lines (19 loc) · 910 Bytes
/
try_catch.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
import java.util.Random;
public class try_catch {
public static void main(String args[])
{
try { //try and Catch block in exception handling
int a = 5;
int b = 0;
int res = a / b; // When dividing by 0 , we get java.lang.ArithmeticException
System.out.print(res); //This will not be printed as the above statement throws an exception
//Once an exception is thrown from try block then exception is treated in catch block and control never returns to try block
}
catch (ArithmeticException e) { //Here we treat the exception
System.out.println("Division by zero");
//To display the description of an exception
System.out.println("Exception:"+e);
}
System.out.print("After Catch execution ");
}
}