Skip to main content

Checked and unchecked Exception

Checked exceptions

The exception which are checked by compiler for smooth execution of the program are called checked exceptions.
Example-:
              1. IOException : This exception is thrown when an input or output operation (e.g., reading or writing to a file) encounters an issue, such as a file not being found or permission denied.

             2. SQLException : It is thrown when a database operation fails, such as trying to execute an invalid SQL query or losing a connection to the database.

            3. ClassNotFoundException : Occurs when the JVM cannot find a class at runtime that is required by the program.

            4.  InterruptedException : Raised when a thread is interrupted while it's waiting, sleeping, or in a blocked state.
 
 Real life Example-:
                Hall ticket missing exception
                pen not working exception
                file not found exception etc.

In our program if there is chance of raising checked exception then compulsory we should handle that checked exception (either by try-catch or by throws key word) otherwise we will get compile time error.


Here's an example of how to handle a checked exception using a try-catch block:


try {
    // Code that may throw a checked exception
    // ...
} catch (IOException e) {
    // Handle the IOException
    // ...
}


Or you can declare that your method may throw a checked exception using the `throws` keyword:


public void readFile() throws IOException {
    // Code that may throw an IOException
    // ...
}


By explicitly handling or declaring checked exceptions, Java enforces error handling and ensures that exceptions are dealt with appropriately, which can lead to more robust and reliable code.


Unchecked exceptions

The exception which are not checked by compiler whether programmer handling are not such type of exception are called unchecked exceptions 

Common examples of unchecked exceptions in programming languages like Java include:

        1. NullPointerException: Occurs when an attempt is made to access or manipulate an object that is null (i.e., it doesn't reference any memory location).

       2. ArrayIndexOutOfBoundsException: Occurs when an attempt is made to access an array element using an index that is outside the valid range.

       3. ArithmeticException: Occurs when mathematical operations result in an undefined or impossible value, such as dividing by zero.

      4. IllegalArgumentException: Occurs when a method is called with an argument that has an illegal or inappropriate value.

     5. ClassCastException: Occurs when an attempt is made to cast an object to a class that is not compatible with its actual type.

     6. UnsupportedOperationException: Occurs when an operation is not supported or implemented by an object or data structure.

Unchecked exceptions are often caused by errors in the program's logic or incorrect usage of APIs, and they indicate a problem that should be fixed in the code. While you are not required to explicitly catch or declare unchecked exceptions, it's a good practice to handle them gracefully to prevent unexpected program crashes and improve the reliability of your software.



Note -:Run type exception and its child classes, error and its child classes are unchecked, except these remaining are checked.

Fully checked exception-:
    A checked exception said to be fully check if and only if all its child class also checked.
    Example-: IOException ,Interrupted Exception
Patrial checked exception-:
   A checked exception, said to be partially if and only if some of its child classes are unchecked exception.
     Example-: Exception ,throwable.

The only possible partially checked exception in Java are. 
  •   Exception
  •   Throwable
IOException -----> checked Exception(fully)
RunTime Exception---->Unchecked
InterruptedException ---->Checked (fully)
Error ----->Unchecked
Throwable ------>Checked(partially)
ArithmeticException ------>Unchecked
Exception -----> checked(partially)
NullPointerException  ----->unchecked
FileNotFoundException ---->checked(fully)

Popular posts from this blog

Java

Codes With Java — Basics Codes With Java Java tutorials & fundamentals About Contact Privacy Basic Fundamentals Java source file structure Import Statement Static Import Packages Data Type Variables Final Variable Declaration and Access Modifier Inner classes applicable modifiers Static Modifier Synchronized Native Transient Volatile Interface Introduction Interface Declaration and Implementation Interface methods and variables Naming Conflicts Interface Marker interface and Ad...

Short Circuite Operators part 4

                                                             Short Circuit Operators In  Java logical operators , if the evaluation of a logical expression exits in between before complete evaluation, then it is known as  Short-circuit . A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned. Short circuit evaluation avoids unnecessary work and leads to efficient processing. 1-: AND(&&) 2-:OR(||) these are exactly same as bitwise operators (&,|) except the following differences. Single Short Circuit Operator(&,|) Both arguments Should be evaluated always. relatively performance is low. Applicable for both boolean and Integral types. Double Short Circuit Operator(...

Operators & Assignment part 3

                                                                                       Operators & Assignment 1-:instanceof Operators 2-:Bitwise Operators                                                                        instanceof Operators we can use instanceof operator to check whether the given object is of a particular type are not. Example-: /* list is a collection of objects. List l=new List(); l.add(Customer); l.add(Student); l.add(Test); Object o=l.get(...