All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Handle Unchecked Exceptions In Java

Last Updated : Mar 11, 2024

How To Handle Unchecked Exceptions In Java

In this article we will show you the solution of how to handle unchecked exceptions in java, exceptions are problems that occur at runtime and cause the application to stop working abruptly.

Keep in mind that exceptions of any kind are always raised at runtime rather than at compile time.

At the time of compilation, no exception is raised. Throwable is the super-class for all errors and exceptions. There is currently a need to deal with them urgently, and the Java language has a concept known as "Exception Handling Techniques" for this purpose.

Step By Step Guide On How To Handle Unchecked Exceptions In Java :-

Take an employee who commutes from home to work. The parent is keeping an eye on him and taking everything, they can think of, including ID card material. Even when the employee is fully informed, they are still under observation.

Now that the employee has left the house, he was somewhat delayed since he had a flat tyre on his car, which caused him to be late for work.

Now, in Java, these desired items that interfere with his daily activities are known as exceptions.

Even if his parents assisted him, even though he had checked those items, in Java, a "checked exception" is referred to be a situation in which the employee received the correct items at home.

Unchecked exceptions are acts that parental access does not have any control over.

In programming languages, a parent or monitoring authority is referred to as a "Compiler" in this context. Checked exceptions are ones that compilers can identify, whereas unchecked exceptions are those that cannot be identified.

These exceptions happen when the programme is running. These are the exceptions that the compiler does not check during compilation.

The Error and Runtime Exception classes in Java have unchecked exceptions. This Exception results from poor programming.

Class of errors It can be challenging to deal with exceptions like StackOverflow and OutOfMemoryError exceptions.

Runtime Try can be used to handle exceptions like IndexoutOfBoundException and Nullpointer Exception, among others. Block, Catch

IndexOutOfBoundsException and NullPointerException are two common Unchecked Exceptions that programmers encounter frequently.

We will put them into practise and talk about how to handle them after they are outlined below with the aid of an example. The two main strategies are as follows:

IndexOutOfBoundsException

NullPointerException

// Importing Classes/Files
import java.io.*;
public class GFG {
// Main Driver Method
public static void main(String[] args)
{
// Inserting elements into Array
int b[] = { 1, 2, 3, 4, 5 };
// Try block for exceptions
try {
// Forcefully trying to access and print
// element/s beyond indexes of the array
System.out.println(a[5]);
}
// Catch block for catching exceptions
catch (ArrayIndexOutOfBoundsException e) {
// Printing display message when index not
// present in a array is accessed
System.out.println(
"Out of index please check your code");
}
}
}
  1. Importing Classes or files
  2. Creating the Main driver method
  3. Inserting the elements into array
  4. Implement the try block of exceptions
  5. Then implement catch block of exceptions
  6. Printing display message
  7. Run the code

Conclusion :-

So, this is how unchecked exceptions are treated in Java. In next tutorials we will learn about treating checked exceptions as well.

I hope this article on how to handle unchecked exceptions in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪