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.
Propagating checked exception (FileNotFoundException) using throws keyword >
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 */
EXCEPTIONS - Top 60 interview questions and answers in java for fresher and experienced - detailed explanation with diagrams Set-1 > Q1- Q25