Differences between Checked (compile time exceptions) and UnChecked (RuntimeExceptions) in java - Definition


Contents of page :
  • checked exceptions
  • unchecked exceptions
  • Differences between checked and unchecked exceptions >



checked exceptions
checked exceptions are also known as compileTime exceptions.
Checked exceptions are those which need to be taken care at compile time.


Benefit of using compiletime Exception >
We cannot proceed until we fix compilation issues which are most likely to happen in program, this helps us in avoiding runtime problems upto lot of extent.
Example- FileNotFoundException - Until we handle this exception, user will face compilation error, because at runtime there is huge probability of file missing in the directory.

Which classes are which exception?
The class Exception and all its subclasses that are not also subclasses of RuntimeException are checked exceptions.

Exception propagation >
For propagating checked exceptions method must throw exception by using throws keyword.

unchecked exceptions

unchecked exceptions are also known as runtime exceptions.
Unchecked exceptions are those which need to be taken care at runtime.

Benefit of using RunTime Exception >
Whenever runtime exception occurs execution of program is interrupted, but by handling these kind of exception we avoid such interruptions and end up giving some meaningful message to user.


Which classes are which exception?
The class RuntimeException and all its subclasses are unchecked exceptions.
Likewise,
The class Error and all its subclasses are unchecked exceptions.

Exception propagation >
unchecked exceptions are automatically propagated in java.


Differences between checked and unchecked exceptions >


Property
checked exception
unchecked exception
1
Also known as
checked exceptions are also known as compileTime exceptions.
unchecked exceptions are also known as runtime exceptions.
2
Should be solved at compile or runtime?
Checked exceptions are those which need to be taken care at compile time.
Unchecked exceptions are those which need to be taken care at runtime.
3
Benefit/ Advantage
We cannot proceed until we fix compilation issues which are most likely to happen in program, this helps us in avoiding runtime problems upto lot of extent.
Whenever runtime exception occurs execution of program is interrupted, but by handling these kind of exception we avoid such interruptions and end up giving some meaningful message to user.
4
Creating custom/own exception

class UserException extends Exception {
   UserException(String s) {
          super(s);
   }
}
By extending java.lang.Exception, we can create checked exception.

class UserException extends RuntimeException {
   UserException(String s) {
          super(s);
   }
}
By extending java.lang.RuntimeException, we can create unchecked exception.

5
For propagating checked exceptions method must throw exception by using throws keyword.
unchecked exceptions are automatically propagated in java.
6
handling checked and unchecked exception while overriding superclass method
If superclass method throws/declare checked exception >
  • overridden method of subclass can declare/throw narrower (subclass of) checked exception (As shown in Program), or
  • overridden method of subclass cannot declare/throw broader (superclass of) checked exception (As shown in Program), or
  • overridden method of subclass can declare/throw any unchecked /RuntimeException (As shown in Program)
If superclass method throws/declare unchecked >
  • overridden method of subclass can declare/throw any unchecked /RuntimeException (superclass or subclass) (As shown in Program), or
  • overridden method of subclass cannot declare/throw any checked exception (As shown in Program),

Which classes are which type of exception?  either
checked or unchecked exception?
The class Exception and all its subclasses that are not also subclasses of RuntimeException are checked exceptions.
The class RuntimeException and all its subclasses are unchecked exceptions.
Likewise,
The class Error and all its subclasses are unchecked exceptions.
7
Most frequently faced exceptions
IOException,
ClassNotFoundException
ArithmeticException ArrayIndexOutOfBoundsException.


/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */


RELATED LINKS>


eEdit
Must read for you :