coderz.py

Keep Coding Keep Cheering!

Exception Handling in Java

java thread class

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.

exception handling
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

Leave a Comment

Your email address will not be published. Required fields are marked *

Advertisement