Exception (or an exceptional event) which occurs when the program is in execution which can change the normal flow of the program. An Exception is the instance of java.lang.
Exception is further classified into two parts:
- It will be checked at compile time.
- If some bad code has been written then compiler will flag it.
- So developer no option other than put special attention to it.
- Checked exception will either be handled or declared.
- Handled means it has to be monitored by “try….catch” block or declared means it has to be thrown back to calling class through keyword “throws”.
- Manily this exception deals with something which is getting accessed from outside like File Access, DB access etc. Don’t worry, I will show you many further examples to explain it properly.
- All checked exceptions are direct subclasses of Exception except RuntimeException. Have a look at exception hierarchy in the picture.
Some Checked Exceptions
This can happen when bad code encounters like
- Some object is null while using it.
- Trying to access some array index which is not been allocated.
- Trying to supply irrelevant parameters which is not expected.
Some Unchecked Exceptions
An Error is a subclass of Throwable that indicates serious(major) problems that a reasonable application should not try to catch. This can also termed as abnormal conditions. The ThreadDeath error, though a “normal” condition, is also a subclass of Error because most applications should not try to catch it. Error also indicated that JVM is not compatible to handle this kind of abnormal situations.