Exception Handling in JAVA

Exception Handling in JAVA

Exception Handling is handling of unexpected events. Every programmer, be an expert can have some errors in the code, JAVA provides a robust way to deal with these errors.

Why do we Use it?

One of the simplest examples is, let us say we make a calculator application and then user wants to perform division operation, he enters some positive number in the numerator but 0 in the denominator(eg.. 3/0). Now, if the developer at the time of making the application has kept in mind about certain exceptions then the user will have a smooth user experience as the application would show a message like infinity, but if the developer forgot about it then the application would terminate abruptly, thereby hampering the user experience.

Code running on production environment without proper exception handling is nightmare for developers when there is issue in live application, as it becomes hard to find what caused the error.

Exception Class Hierarchy

All exception types are subclasses of class Throwable, which is at the top of exception class hierarchy.

exceptional_handling_java

Errors

Errors are the most serious exceptional conditions that you can run into. They are often irrecoverable from and there's no real way to handle them. The only thing we, as developers, can do is optimize the code in hopes that the errors never occur.

Errors can occur due to human and environmental errors. Creating an infinitely recurring method can lead to a StackOverflowError, or a memory leak can lead to an OutOfMemoryError.

Checked Exceptions

Checked Exceptions are the exceptions that we can typically foresee and plan ahead in our application. These are also exceptions that the Java Compiler requires us to either handle-or-declare when writing code.

The handle-or-declare rule refers to our responsibility to either declare that a method throws an exception up the call stack - without doing much to prevent it or handle the exception with our own code, which typically leads to the recovery of the program from the exceptional condition.

This is the reason why they're called checked exceptions. The compiler can detect them before runtime, and you're aware of their potential existence while writing code.

For example FileNotFoundException occurs if File that needs to be opened is not found.

Unchecked/Runtime Exceptions

Unchecked Exceptions are the exceptions that typically occur due to human, rather than an environmental error. These exceptions are not checked during compile-time, but at runtime, which is the reason they're also called Runtime Exceptions.

For example trying to retrieve an element from the Array. We should check the length of array first before trying to retrieve the element otherwise it might throw ArrayIndexOutOfBoundException at runtime.

Handling Exceptions in JAVA

We can handle exceptions in JAVA using :-

  1. try-catch-finally
  2. throws
  3. throw
Unchecked Exceptions Description
ArithmeticException Arithmetic Error, like divide by zero.
ArrayIndexOutOfBoundsException Accessing an element out of the size of an array or accessing an illegal index.
InputMisMatchException Thrown by Scanner, indicating that the token retrieved does not match the pattern of the expected type.
NullPointerException Trying to access an object reference that has null value or not allocated any space in memory.
NumberFormatException It is thrown by parse methods, when they are unable to convert string into a number.
StringIndexOutOfBounds Similar to arrayindexoutofboundsexception, but for an array of strings.

 

Checked Exceptions Description
ClassNotFoundException Occurs when class is not found in classpath.
FileNotFoundException  Occurs when file is not found
InterruptedException This exception is thrown when a thread is interrupted.

Share Me!
  •  
  •  
  •  
  •  
  •  
  •