Categories: Java

Exception Handling in Java

One of the efficient ways to deal with runtime failures in Java is through exception handling, which helps to maintain the application’s normal flow. Runtime issues like ClassNotFoundException, IOException, SQLException, RemoteException, etc. are handled via Java’s exception handling framework.

What is an exception?
  • An exception is a disruptive occurrence that takes place at run time, or during the execution of a program, which interrupts the regular flow of the program’s instructions.
  • The application has the ability to detect and manage exceptions.
  • An object is created when an exception occurs within a method.
  • The exception object is the name of this item. It includes details about the exception, including its name, description, and the program’s status at the time the exception occurred.
Exception Hierarchy:

Throwable, the base class in the hierarchy, is a subclass of all exception and error kinds. One branch is headed by Exception. This class is utilized in special circumstances that user programs should detect. An illustration of one of these exceptions is NullPointerException. The Java Run-Time System (JVM) uses a different branch called Error to denote faults relating to the run-time environment itself (JRE). One such issue is the StackOverflowError.

Types of Java Exceptions:

There are mainly two types of exceptions: checked and unchecked. An error is considered as the unchecked exception. However, according to Oracle, there are three types of exceptions namely:

  • Checked Exceptions: Because the compiler checks these exceptions at compile-time, they are sometimes known as compile-time exceptions.
  • Unchecked Exceptions: The unchecked exceptions are just opposite to the checked exceptions. These exceptions won’t be checked by the compiler during compilation. In other words, even if we didn’t manage or declare an unchecked exception that a program throws, it wouldn’t cause a compilation error.
  • Errors: The Java virtual machine (JVM) running out of memory, memory leaks, stack overflow errors, library incompatibility, infinite recursion, etc. are examples of unrecoverable circumstances that are represented by errors. We shouldn’t try to handle errors because they are typically out of the programmer’s control.
The advantages of Exception Handling in Java are as follows:
  1. Provision to Complete Program Execution
  2. Easy Identification of Program Code and Error-Handling Code
  3. Propagation of Errors
  4. Meaningful Error Reporting
  5. Identifying Error Types

Note: also read about the StringTokenizer in Java

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

2 days ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

3 weeks ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

3 weeks ago

Staircase Climbing Ways

There is a staircase with N steps, and you can ascend either 1 step or…

4 weeks ago

Autocomplete System Implementation

Build an autocomplete system that, given a query string s and a set of possible…

4 weeks ago

Job Scheduler Implementation

Design a job scheduler that accepts a function f and an integer n. The scheduler…

4 weeks ago