Difference between the marked and unverified exception in Java

Difference between the marked and unverified exception in Java


"Throwable" is the main class of the Error and Exception classes. The "RuntimeException" class and its subclasses, the "Error" class and its secondary classes are the "Unverified Exceptions", while the remaining subclasses of the "Exception" class except "RuntimeException" are the exceptions checked. The basic difference between the marked and unverified exception is that the compiler check the exceptions verified, while the compiler does not mark the exceptions not verified

Let's discuss the other differences between exceptions marked and unverified with the help of the comparison chart.

Comparative graph

Basis for comparison Exception checked Unmarked exception
BASIC The compiler checks the exception checked. The compiler does not mark the Unchecked exception.
Exception class Except in the "RuntimeException" class, all the secondary classes of the "Exception" class, and the "Error" class and their secondary classes are Checked Exception. The "RuntimeException" class and its secondary classes are "Unverified Exceptions."
Driving If we don't handle the marked exception, then the compiler objects. Even if we do not handle the exception without checking, the compiler does not object.
Compilation The program is not compiled if there is a marked exception not controlled in the program code. The program compiles successfully even if there is an uncontrolled uncontrolled exception in the program code.

Definition of proven exception

The exceptions checked are those exceptions that the compiler verifies for trouble-free execution of the program. The compiler checks if the program can handle the verified exception or not. If the code cannot handle the exception, a compilation error occurs. All subclasses of the "Exception" class, except the RuntimeException class, are the exceptions checked. Let us study the exception marked with an example.

 import java.io. *; clase Ejemplo {public static void main (String args ()) {FileInputStream fis = null; fis = new FileInputStream ("B: /myfile.txt"); // Este constructor FileInputStream (nombre de archivo de archivo) lanza la excepcin FileNotFoundException, que es una excepcin comprobada. int k; while ((k = fis.read ())! = -1) {// El mtodo read () de la clase FileInputStream tambin lanza una excepcin comprobada: IOException. System.out.print ((char) k); } fis.close (); // El mtodo close () cierra la secuencia de entrada de archivos que lanza IOException. }} 

In the previous code, we try to open, read and display the contents of the files. It may happen that the file is not present, then the FileNotFoundException exception occurs, which is a proven exception. If the marked exception is not handled, the compiler will not allow the program to compile without problems. Compile time error will be displayed. Therefore, for a smooth compilation, the exception checked must be captured or declared as thrown.

Unverified exception definition

Unmarked exceptions are those exceptions that are not verified by the compiler. The compiler successfully compiles the program even if the exceptions are not handled by the code. The "Error" class and its secondary classes, the "Runtime" class and its subclasses are the unverified exceptions.

Let's look at an example of an unmarked exception.

 clase Ejemplo {public static void main (String args ()) {int arr () = {1, 2, 3, 4, 5}; System.out.println (arr (7)); // ArrayIndexOutOfBoundsException. }} 

In the previous code, you can see that there is an ArrayIndexOutOfBoundsException, since I am trying to access the element that does not exist. As this is an untested exception, a compile-time error will not occur and the file will be compiled without any errors. But the program will not run until the exception is handled. Therefore, for a smooth execution, exceptions must be captured or declared to be thrown

Key differences between the marked and unverified exception

  1. Marked exceptions are in the compiler's knowledge, while unverified exceptions are not in the compiler.
  2. Except for the RuntimeException and Error class, all classes are verified exceptions. On the other hand, the RuntimeException and Error classes are unmarked exceptions.
  3. If the marked exceptions are not handled by the code, then the compiler objects. On the other hand, if we do not handle unverified exceptions in the code, the compiler does not object.
  4. If marked exceptions occur in the code, the code will not compile while, even if uncontrolled exceptions are not handled by the code, the compiler still compiles the code.

Note:

The exception marked or not activated occurs obligatorily during the execution time. The compiler only activates or deactivates them during compile time.

Conclusion:

Marked and unverified exceptions must be handled to run the program without problems.