The perfect place for easy learning...

Java Programming

×

Topics List


Uncaught Exceptions in Java





In java, assume that, if we do not handle the exceptions in a program. In this case, when an exception occurs in a particular function, then Java prints a exception message with the help of uncaught exception handler.

The uncaught exceptions are the exceptions that are not caught by the compiler but automatically caught and handled by the Java built-in exception handler.

Java programming language has a very strong exception handling mechanism. It allow us to handle the exception use the keywords like try, catch, finally, throw, and throws.

When an uncaught exception occurs, the JVM calls a special private method known dispatchUncaughtException( ), on the Thread class in which the exception occurs and terminates the thread.

The Division by zero exception is one of the example for uncaught exceptions. Look at the following code.

Example
import java.util.Scanner;

public class UncaughtExceptionExample {
        
    public static void main(String[] args) {
                
        Scanner read = new Scanner(System.in);
        System.out.println("Enter the a and b values: ");
        int a = read.nextInt();
        int b = read.nextInt();
        int c = a / b;
        System.out.println(a + "/" + b +" = " + c);
        
    }
}
    

When we execute the above code, it produce the following output for the value a = 10 and b = 0.

Uncaught exceptions in java

In the above example code, we are not used try and catch blocks, but when the value of b is zero the division by zero exception occurs and it caught by the default exception handler.