What are checked and unchecked exceptions in Java?

Posted On: Dec 18, 2020

 

An Exception is a run time error especially occurring at execution time.

Following are the two types of Exceptions in JAVA:

  1. Checked Exception
  2. Unchecked Exception

In Java, The exceptions that are checked during the compilation time are termed as Checked exceptions to verify that a method that is throwing an exception contains the code to handle the exception with the try-catch block or not. if there is no code to handle them, then the compiler checks whether the method is declared using the throws keyword. And, if the compiler finds neither of the two cases, then it gives a compilation error.

A checked exception extends the Exception class. For instance, if we write a program to read data from a file using a FileReader class and if the file does not exist, then there is a FileNotFoundException.

An exception that checks during the execution of a program is called an unchecked or a runtime exception. The unchecked exceptions occur when the user attempts to access an element with an invalid index, calling the method with illegal arguments, etc. Unchecked exceptions are checked during the runtime. Therefore, the compiler does not check whether the user program contains the code to handle them or not.

For instance, if a program attempts to divide a number by zero. Or, when there is an illegal arithmetic operation. Suppose, we declare an array of size 10 in a program, and try to access the 12th element of the array, or with a negative index like -5, then we get an ArrayIndexOutOfBounds exception.

    Related Questions

    Please Login or Register to leave a response.

    Related Questions

    HCL Java Developer Interview Questions

    What is Multithreading in Java?

    Multithreading is a programmable approach to achieve multitasking. Multithreading in Java is a process of executing multiple threads cumulatively. A thread is the smallest unit of processing which is ...

    HCL Java Developer Interview Questions

    What use of intern() method in String?

    In Java, intern() is a native method of the String class. The String.intern () returns a reference to the equal string literal present in the string pool. The intern () method is applicable to String ...

    HCL Java Developer Interview Questions

    What is Collection Framework?

    A Java collection framework is an architecture that was added to Java 1.2 version. The Collection Framework provides a well-designed set of classes and interfaces for storing and manipulating a group ...