
how to use throws exception in java?

The throws keyword is used to indicate that the method raises particular type of exception while being processed.
public class StringTest {
public static void divide() throws ArithmeticException {
int x = 10, y = 0;
int z = x / y;
}
public static void main(String[] args) {
divide();
}
}
Output:-
Exception in thread "main" java.lang.ArithmeticException: / by zero
Description:- Here is an example of throws clause. We have created a class named ThrowsExample. In the main method, we have declared two variables of integer type and divide the first value by another which is 0. The throws keyword performs exception handling and display the 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.