Q. Write a Java program to use the try and catch and finally block.
Answer:
In this example, we are implementing try and catch block to handle the exception. The error code written is in try block and catch block handles the raised exception. The finally block will be executed on every condition.
Output:

Answer:
In this example, we are implementing try and catch block to handle the exception. The error code written is in try block and catch block handles the raised exception. The finally block will be executed on every condition.
ExceptionTest.java
class ExceptionTest
{
public static void main(String[] args)
{
int a = 40, b = 4, c = 4;
int result;
try
{
result = a / (b-c);
}
catch (ArithmeticException ae)
{
System.out.println("Cannot divided by zero."+ae);
}
finally
{
System.out.println("finally block");
}
result = a / (b+c);
System.out.println("Result: "+result);
}
}
Output:



