forked from ranjansharma255/Java_Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThrowDemo.java
More file actions
30 lines (30 loc) · 908 Bytes
/
ThrowDemo.java
File metadata and controls
30 lines (30 loc) · 908 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
import java.lang.*;
class ThrowDemo
{
static int x=5,y=0,z=0;
static int div()
{
try{
if(y==0)
throw new ArithmeticException("Division By Zero"); //if no catch block //was there then this object was sent to the jvm, here catch clause is //there so this is sent to the catch block with the message along with //the object "division by zero"
else
return (x/y);
}
catch(ArithmeticException e)
{
System.out.println("Arithmetic Exception caught at div()" + e);
throw e; // this will return the exception to place where it is called and //the place where it is called is in try block so its corresponding catch //block will be executed
}
}
public static void main(String[] args)
{
try{
z=div();
System.out.println("z value = " + z);
}
catch(ArithmeticException ae)
{
System.out.println("Exception caught at main()" + ae);
}
}
}