
How can we use try and catch block in exception handling?

Here is an example of Exception handling.
Example:
public class TryCatchExample{
public static void main(String [] args){
int a= 10;
int b= 0;
int x;
try{
x=a/b;
}catch (Exception er){
System.out.println("Division by zero");
System.out.println(er);
}
}
}
Result Display:
Division by zero Java.lang.ArithmeticException: / by zero
Description:- We have created a class named TryCatchExample. In the main method, we have declared some variables of integer type. Then to perform exception handling, we have used try and catch block where we have divided the integer value by 0 in the try block. This results in an Arithmetic Exception. The catch block will show this exception.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.