Exception handling in Java

Posted in Exception Handling By Raj On December 25, 2017


What happens when the program or software throws several errors or not responding to end user while using software which was built by you, the people who are using your software gets frustrated and they will not come back to visit your website, you might have written very good code but what if you forgot to implement exception handling properly? 

While executing Java program if something happened it should not impact the normal flow of execution, there you need to use exception handling mechanism

In Java, mainly two types exceptions

  • checked or compile time exceptions
  • unchecked or runtime exceptions

Checked Exceptions

The exceptions which occur at compile time are checked exceptions, 

If you see the below real-time example, I am trying to read excel file using FileInputStream class but, think like this what happens if there is no file exists in the data folder, the entire program will fail at runtime and you will get unexpected results so we need to handle this at compile time itself

here, the FileInputStream class throws FileNotFound Exception 


Unchecked Exceptions

The exceptions which occur at runtime are unchecked exceptions, the example for unchecked exceptions are if you are trying to read cell data which is in the excel file, and the value is not present in the particular cell then it throws null pointer exceptions at runtime