//Arithmetic Exception
public class JavaExceptionExample
{
public static void main(String args[])
{
try
{
//code that may raise exception
int data=100/0;
}
catch(ArithmeticException e)
{System.out.println(e);
System.out.println(“Hello”);
}
//rest code of the program
System.out.println("rest of the code...");
}
}
Output:
Exception in thread main java.lang.ArithmeticException:/ by zero
rest of the code...
// Java program to demonstrate finally block in java When exception rise
and handled by catch
import java.io.*;
class GFG
{
public static void main(String[] args)
{
try {
System.out.println("inside try block");
// Throw an Arithmetic exception
System.out.println(34 / 0);
}
// catch an Arithmetic exception
catch (ArithmeticException e)
{
System.out.println("catch : exception handled.");
}
// Always execute
finally {
System.out.println("finally : i execute always.");
}
}
}
// Java program to demonstrate finally block When exception rise and not
handled by catch
import java.io.*;
class GFG {
public static void main(String[] args)
{
try {
System.out.println("Inside try block");
// Throw an Arithmetic exception
System.out.println(34 / 0);
}
// Can not accept Arithmetic type exception
// Only accept Null Pointer type Exception
catch (NullPointerException e) {
System.out.println(
"catch : exception not handled.");
}
// Always execute
finally {
System.out.println(
"finally : i will execute always.");
}
// This will not execute
System.out.println("i want to run");
}
}