Page 389 - CITS - Computer Software Application -TT
P. 389
COMPUTER SOFTWARE APPLICATION - CITS
int data=50/0; //may throw exception
// if exception occurs, the remaining statement will not exceute
System.out.println(“rest of the code”);
}
// handling the exception
catch(ArithmeticException e)
{
System.out.println(e);
}
}
}
Output
java.lang.ArithmeticException: / by zero
Here, we can see that if an exception occurs in the try block, the rest of the block code will not execute.
Example 4
Here, we handle the exception using the parent class exception.
TryCatchExample4.java
public class TryCatchExample4 {
public static void main(String[] args) {
try
{
int data=50/0; //may throw exception
}
// handling the exception by using Exception class
catch(Exception e)
{
System.out.println(e);
}
System.out.println(“rest of the code”);
}
}
Output
java.lang.ArithmeticException: / by zero
rest of the code
376
CITS : IT&ITES - Computer Software Application - Lesson 101 - 108