Exception Handling in Java - 1

Raviteja Blogs
3 min readApr 28, 2022

--

Imagine you are going on a road trip with your girlfriend and suddenly the tyre of your 🚗 gets punctured 😩. What would you do in that unexpected situation? Will you cancel your romantic trip or search for an alternative when this kind of unexpected situation occurs? Obviously, who wants to miss the trip just for a common exception that occurs frequently.

Exceptions are such kind of unwanted unexpected events that disturbs the normal flow of our program which leads to abnormal termination. It is highly recommended to handle exceptions. The main objective of exception handling is graceful termination(normal) of the program.

Exception handling doesn’t mean stopping exceptions (unwanted events) from occurring. We have to define an alternative way to continue the rest of the program normally. This way of defining alternatives is nothing but exception handling.

Example: Suppose we need to read data from a remote file. At runtime, if that file is not available instead of terminating the program abnormally we have to provide a local file to continue the rest of the program normally

Understanding how exceptions are handled internally

We all know how the recursive stack gets filled up. We will analyse how default Exception Handling is done in java.

  • If an exception is raised in any method then that method is responsible to create an Exception object with the following information
    - Name of the Exception
    - Description of the Exception
    - Location of the Exception (stack trace)
  • After creating the exception object, the method handovers the object to JVM.
  • JVM checks whether the method contains any exception handling code or not. If not found JVM terminates the method abnormally and removes the corresponding entry from the stack.
  • JVM recursively identify the caller methods for abnormally terminated methods and checks whether any code is written to handle an exception. If not found all such entries will be removed from the stack.
  • This process same for the main function also. If the main method also doesn’t contain any exception handling code then JVM terminates the main abnormally and removes it from the stack.
  • Then JVM handovers the responsibility of exception handling to the default exception handler.
  • The default exception handler just prints exception information to the console in the following format and terminates the program abnormally.

“Exception in thread ‘xxx (main)’. Name of the exception: description Location of the exception (stack trace)”

java -cp /tmp/PGmraqCkod ExceptionDemo
Exception in thread "main" java.lang.ArithmeticException: / by zeroat ExceptionDemo.bigShit(ExceptionDemo.java:9)at ExceptionDemo.smallShit(ExceptionDemo.java:6)at ExceptionDemo.main(ExceptionDemo.java:3)

Note: Exceptions always occur at the runtime only.

Exception Hierarchy:

Throwable class acts as a root for exception hierarchy. The throwable class contains Exception and Error as child classes.

Exceptions are the result of coding mistakes in our program and these are recoverable using exception handling methods

Errors are not caused by our program but due to a lack of system resources and these are not recoverable. Ex: OutOfMemoryError.

In the next part 2, we will discuss checked vs unchecked exceptions.

Clap and Share 😁. You can connect with me on LinkedIn, Twitter, and Instagram.

--

--