Exception Handling Introduction

Exception

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:

  1. Checked
  2. Unchecked

    ExceptionClassHierarchy
    Exception Class Hierarchy

Checked Exception

  1. It will be checked at compile time.
  2. If some bad code has been written then compiler will flag it.
  3. So developer no option other than put special attention to it.
  4. Checked exception will either be handled or declared.
  5. 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”.
  6. 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.
  7. All checked exceptions are direct subclasses of Exception except RuntimeException. Have a look at exception hierarchy in the picture.

Some Checked Exceptions

  1. Exception
  2. FileNotFoundException
  3. IOException
  4. DataAccessException
  5. SocketException
  6. ClassNotFoundException
  7. InvocationTargetException

Unchecked Exception

Unchecked exceptions are not like Checked Exceptions. It means when program is encountering any type of unchecked exception then this will be encountered at runtime. It will crash the program. All unchecked exceptions are direct subclasses of RuntimeException.

This can happen when bad code encounters like

  1. Some object is null while using it.
  2. Trying to access some array index which is not been allocated.
  3. Trying to supply irrelevant parameters which is not expected.

Some Unchecked Exceptions

  1. NullPointerException
  2. IndexOutOfBoundsException
  3. ArrayIndexOutOfBounds
  4. ArithmeticException

Error

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.

error class hierarchy
error class hierarchy
If you like the post then please socialize it. Happy Sharing. Loudly Subscribing.

One Comment

  1. Pingback: Java Made Easy - Technical Jungle

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.