The knowledge sharing zone - Guruzon.com

Difference between checked and unchecked exception

This tutorial contains the difference between checked and unchecked exception in java with example.
reviews

Following is the difference between checked and unchecked exception in java with example.

Difference between checked and unchecked exception

Checked Exception

 Unchecked Exception

  • A checked exception is an exception that must be either caught and handled or listed in the throws clause of any method that may throw or propagate it.
  • This establishes a set of exceptions that must be formally acknowledged in the program one way or another.
  • A Java exception that does not need to be caught or dealt with if the programmer so chooses.
  • Unchecked exceptions can be ignored completely in the code if desired.
If your code fails to either handle a checked exception or declare that it is thrown, your code won't compile. But with unchecked exceptions or objects of type Error, it doesn't matter to the compiler
whether you declare them or handle them, do nothing about them, or do some combination of declaring and handling.

JVM enforces to handle or catch it:

Any exception which is must to handle or catch it, while writing the program is called checked exception.

JVM does not enforce to handle or catch it:

Any exception which is not mandatory to handle or catch it, while writing the program or executing the program is called unchecked exception.

Checked exception classes:

Checked exceptions are all descendants of Exception class.

Unchecked exception classes:

Unchecked exceptions are all descendants of RuntimeException class.

Create own checked exception:

You can also define custom checked exception class by deriving new class from Exception class.

Create own unchecked exception:

You can also define custom unchecked exception class by deriving new class from RuntimeException class.

throw clause:

The throws clause on a method header must be included for checked exceptions that are not caught and handled in the method.

throw clause:

The throws clause on a method header is not mandatory to be included for unchecked exceptions that are not caught and handled in the method.

When to use:

You can use checked exception for two different cases:

  1. When you want to force (must) callee method / api to handle the exception which might occur during the execution of program.
  2. Some checked exception can be critical, Should result in program termination, For example - IOException

When to use:

Usually unchecked exception are used for different runtime exception scenarios like:

  1. User input validation
  2. Business logic or rules validation etc..
For example, ClassNotFoundException, IllegalAccessException, NoSuchMethodException etc.. For example, NullPointerException, ArithmeticException, IndexOutOfBoundException etc..

Example of custom checked exception:

class InvalidConfigFileException extends Exception {
   
   InvalidConfigFileException(String errorMsg) {
      super(errorMsg);
   }
}

Example of custom unchecked exception:

class InvalidUsernamePasswordException extends RuntimeException {

   InvalidUsernamePasswordException(String errorMsg) {
      super(errorMsg);
   }
}

Method overriding:

  • The overriding method can not throw new checked exception or broader checked exception.
  • You can throw fewer or narrower checked exceptions, or any unchecked exception or completely remove throw clause in the overriding method (child class).
  • If super class is throwing the IOException for abc() method, Child class can either omit or write child exception of IOExcetion like FileNotFoundException or any unchecked exception.

Method overriding:

  • The overriding method can throw any unchecked (runtime) exception, Regardless of whether the overridden method declares the exception.
  • You can throw fewer or narrower unchecked exceptions or completely remove throw clause in the overriding method (child class).
  • If super class is throwing the RuntimeException for xyz() method, Child class can either omit or write child exception of RuntmeExcetion like NullPointerException.

Key concepts about Unchecked and Checked Exception

  • Errors and exceptions are objects that represent unusual or invalid processing.
  • The messages printed when an exception is thrown provide a method call stack trace.
  • Each catch clause handles a particular kind of exception that may be thrown within the try block.
  • The finally clause is executed whether the try block is exited normally or because of a thrown exception.
  • If an exception is not caught and handled where it occurs, it is propagated to the calling method.
  • A programmer must carefully consider how and where exceptions should be handled, if at all.
  • A new exception is defined by deriving a new class from the Exception class or one of its descendants.
  • The throws clause on a method header must be included for checked exceptions that are not caught and handled in the method.
  • An unchecked, or run-time, exception need not be caught in a catch block or declared in a throws clause. These exceptions usually indicate that something is wrong with your code and that you should fix it.

How do you know whether exception is checked or unchecked ?

  • You can check the documentation for the Java class library to know the base class of the generated exception, Based on that you can decide whether generated exception is checke or unchecked.
  • If generated exception is subclass of RuntimeException then It's of type unchecked exception.
  • Otherwise If generated exception is subclass of Exception class And not the subclass of RuntimeException then It's of type checked exception.

 

You would also like to read:

What is Exception and Error
Difference between compile time and runtime exception
Use of throw and throws keywords with example
JVM and Programmatically Thrown Exceptions
How to create custom check and unchecked exception
Exception types in java

Comments :


thanks a lot..

up vote icon down vote icon

Nice tutorial.. thanks..

up vote icon down vote icon

awesome article ..

up vote icon down vote icon
Name :
Email :
Comment :
Verify Text :
capch image, refresh page if not loaded somehow