The program shown below throws an ArithmeticException when the program tries to divide by zero. The program crashes and prints out information about the exception:
public class DivideByZero
{
public static void main (String[] args)
{
int numerator = 23;
int denominator = 0;
// the following line produces an AritmeticException
System.out.println(numerator/denominator);
System.out.println("This text will not print");
}
}
Run Output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at DivideByZero.main(DivideByZero.java:10)
The first line of the output tells which exception was thrown and gives some information about why it was thrown.
The rest of the output tells where the exception occurred and is referred to as a call stack trace. In this case, there is only one line in the call stack trace, but there could be several, depending on where the exception originated. The trace line gives the method, file, and line number where the exception happened.