What are checked (compile time exceptions) in java




Contents of page >
  • 1) What are checked exceptions in java
  • 2) Advantage/Benefit of using checked/compiletime Exception in java >
  • 3) Example of  checked exceptions in java
  • 4) Which classes are which checked exceptions in java?
  • 5) Propagating checked exception (FileNotFoundException) using throws keyword in java >

1) What are checked exceptions in java
checked exceptions are also known as compileTime exceptions.
Checked exceptions are those which need to be taken care at compile time.


2) Advantage/Benefit of using checked/compiletime Exception in java >
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 in java.

3) Example of  checked exceptions in java
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.

Most common and frequently occurring checked (compile time) in java >


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


5) Propagating checked exception (FileNotFoundException) using throws keyword in java >
For propagating checked exceptions method must throw exception by using throws keyword.
Now, i’ll be explaining you how checked exception was propagated.
Let’s see step by step what happened in above program >
  • JVM called main method
  • step 1 - main called method1()
  • step 2 - method1 called method2()
  • step 3 - method2 called method3()
  • step 4 - method3 propagated exception to method2() using throws keyword.[because, checked exceptions are not propagated automatically]
  • step 5 - method2 propagated exception to method1() using throws keyword.[because, checked exceptions are not propagated automatically]
  • step 6 - method2 propagated exception to main() using throws keyword.[because, checked exceptions are not propagated automatically]
  • main() propagated exception to JVM using throws keyword.[because, checked exceptions are not propagated automatically]






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


RELATED LINKS>


Labels: Core Java
eEdit
Must read for you :