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

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago