All TalkersCode Topics

Follow TalkersCode On Social Media

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

Unchecked Exception In Java

Last Updated : Mar 11, 2024

Unchecked Exception In Java

In this article we will show you the solution of unchecked exception in java, these are the exceptions that the compiler does not check. All exceptions in C++ are unchecked, meaning the compiler is not required to handle or specify the exception.

It is the responsibility of the programmers to specify or catch the exceptions and act civilly. Error and RuntimeException classes in Java have unchecked exceptions, while throwable classes contain checked exceptions.

Think about the Java programme below. It successfully compiles, but when executed, it throws an ArithmeticException. Due to the fact that ArithmeticException is an unchecked exception, the compiler permits it to be compiled.

An execution-time exception is one that cannot be checked. These generally go by the name of runtime exceptions. These include programming flaws like some genuine logical mistakes or inappropriate usage of API.

Runtime exceptions are absolutely not taken into account during the compilation process.

An ArrayIndexOutOfBoundsExceptionexception arises if you declare an array of size 5 in your programme and attempt to call the 6th element of the array.

Step By Step Guide On Unchecked Exception In Java :-

In Java, unchecked exceptions are those whose handling is not checked at compile time. These errors are the result of poor programming.

A compilation error won't be reported by the software.

RuntimeException is a class that all Unchecked exceptions are direct subclasses of.

Simple Illustration like you have designed an online form that takes user input. It's in free-form text. In the case of the email, user name, or phone number fields, users may input any incorrect value.

There are increased chances of getting a Runtime Validation Exception when running an application in production if there is no client-side validation.

Errors can occur during database operations or when changing a field's format.

Types of Unchecked Exceptions are listed below:

  • NullPointerException
  • ArrayIndexOutOfBound
  • IllegalArgumentException
  • IllegalStateException

An unchecked exception, in contrast to a checked exception, denotes a programming error rather than an incorrect circumstance that could possibly arise during the proper use of an API.

The compiler cannot test for these kinds of issues at compile time because it cannot foresee logical mistakes that only occur at runtime they are known as "unchecked" exceptions for this reason.

The cause of unchecked exceptions is flawed logic, which can appear anywhere in a software application.

A developer might call a method on a null object, for instance, and get an unchecked NullPointerException. The unchecked ArrayIndexOutOfBoundsException happens if a developer tries to access an element of an array that doesn't exist.

// Java Program to Illustrate Un-checked Exceptions
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Here we are dividing by 0
// because it is mathematically incorrect
int x = 0;
int y = 10;
int z = y / x;
}
}
  1. Creating a Main Class CFG
  2. Creating Main driver method public static void main(String args[])
  3. Next step we will divide by zero, which is mathematically incorrect
  4. Run the code

Conclusion :-

This is how we have implemented unchecked exception with the help of an example. In next tutorial we will study about list of checked as well as unchecked exceptions in Java.

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

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪