In Java, an unchecked exception is an unexpected error that occurs at runtime and leads to the termination of the program execution. These exceptions are also called runtime exceptions and are ignored during code compilation. Examples of unchecked exceptions include ArithmeticException
, ArrayIndexOutOfBoundsException
and so on.
Let's see an example of an ArithmeticException
:
class Example {public static void main( String args[] ) {int m = 10;int n = 0;// performing divide by zeroint r = m/n;System.out.println(r);}}
Example
.main()
function.m
and n
And initialize them.divide by zero
operation by dividing m
with n
and store the result in the variable, r
. System.out.println
to print the value stored in r
.At the time of compilation, the divide by zero
will not be caught since there's no syntactical mistake. However, we know it is arithmetically incorrect because division by zero is not possible. Therefore, the compiler allows the code to compile. However, during the execution, this error is caught, and it throws an ArithmeticException
, which leads to the termination of our program execution.