Home / Java / Java Exceptions Handling

Java Exceptions Handling

An exception is a problem that disturbs the normal flow of the program execution, and forces the program to terminate.

There are several reasons due to which an exception arises. Below are mentioned some scenarios in which an exception may occur.

  • User has entered invalid data.
  • A file that needs to be opened but does not exist.
  • A network connection has been lost in the middle of communication
  • JVM has run out of memory.

There are mainly three errors due to which an exception may arise.

  • User errors.
  • Programmer Error.
  • Physical Resource have failed in some manner.

Base on these there are three types of exceptions in java. You need to understand these three types of exceptions to know how exceptions can be handled in java.

Checked Exceptions

A checked exception is a kind of exception that occurs at compile time also known as compile time exceptions. These exceptions must be handled by the programmer and cannot be ignored.

For example if you want to read a file using FileReader class in your program to read data from a file. If the file name provided in the constructor does not exist then a FileNotFound Exception occurs and compiler prompts programmer to handle this exception.

Example – FileNotFound Exception

Output

File Not Found Exception - Java Exceptions
File Not Found Exception – Java Exceptions

 

Unchecked Exceptions

A type of exceptions that occurs at the execution time also called RunTime Exceptions. These kind of exceptions include improper use of an API or logical Error. These kind of exceptions are ignored at the compile time.

For example if you have declared an array of size 5 and you are trying to access 6th element of an array, then an array out of bound exception occurs. 

Example – Arrayoutofbound Exception

Output

Arrayoutofbound Exception - Java Exceptions

If you compile the program the compiler does not prompt any exception at compile time but when we try to access the 6th element of the array i.e 5th index, you will see ArrayIndexOutOfBoundsExceptions. 

Errors

These are not exceptions at all, but problems that arise beyond the control of the user or the programmer. Errors are typically ignored in your code because you can rarely do anything about an error. For example, if a stack overflow occurs, an error will arise. They are also ignored at the time of compilation.

Handling Exceptions

We can catch an exception using the combination of try and catch keyword. We use try/catch block around the code that might cause an exception. Code within a try/catch block is referred as protected code. Try catch block looks like this

Example

In this example we will handle array out of bounds exception that is a kind of un-checked exception.

This code will produce the following exception

Exception Handling In Java

Multiple Catch Block

A try block may have multiple catch blocks. The syntax for multiple catch block is as follow.

In above code a try block is followed by multiple catch blocks. Now if an exceptional event is fired it is sent to the first catch block, if it does not matches it is sent to the next one and so on.

Finally Block

A finally block is executed if the no exception is fired.

Conclusion

I hope after reading this, you would be able to handle exception in java language. Let me summarize all this exceptions disturbs the normal execution of the program flow. There are three types of exceptions Checked Exceptions, Un-Checked Exceptions and Errors. Checked Exceptions occurs at compile time and programmer must solve these exceptions. Un-Checked Exceptions occurs at run time. Errors are not a kind of exceptions and can be ignored. To overcome this problem we use try/catch block a try/catch block can be followed by multiple catch blocks.

Share This:

About Hasnain