
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create Custom Unchecked Exception in Java
In Java, the exceptions are of two types: checked and unchecked exceptions. A checked exception is an exception that occurs at compile time; these are also called compile-time exceptions. An unchecked exception occurs at the time of execution. These are also called Runtime Exceptions.
In this article, we will learn to create a custom unchecked exception in Java. We can create a custom unchecked exception by extending the RuntimeException class in Java.
What are unchecked exceptions?
The unchecked exceptions inherit from the Error class or the RuntimeException class. Many programmers feel that we cannot handle these exceptions in our programs because they represent the type of errors from which programs cannot be expected to recover while the program is running. When an unchecked exception is thrown, it is usually caused by misuse of code, passing a null or otherwise incorrect argument.
Syntax
Below is the syntax for a custom runtime exception:
public class MyCustomException extends RuntimeException { public MyCustomException(String message) { super(message); } }
Implementing an Unchecked Exception
The implementation of a custom unchecked exception is almost similar to a checked exception in Java. The only difference is that an unchecked exception has to extend RuntimeException instead of Exception.
Example
Below is an example of implementing thecustom unchecked exception in Java:
public class CustomUncheckedException extends RuntimeException { /* * Required when we want to add a custom message when throwing the exception * as throw new CustomUncheckedException(" Custom Unchecked Exception "); */ public CustomUncheckedException(String message) { // calling super invokes the constructors of all super classes // which helps to create the complete stacktrace. super(message); } /* * Required when we want to wrap the exception generated inside the catch block and rethrow it * as catch(ArrayIndexOutOfBoundsException e) { * throw new CustomUncheckedException(e); * } */ public CustomUncheckedException(Throwable cause) { // call appropriate parent constructor super(cause); } /* * Required when we want both the above * as catch(ArrayIndexOutOfBoundsException e) { * throw new CustomUncheckedException(e, "File not found"); * } */ public CustomUncheckedException(String message, Throwable throwable) { // call appropriate parent constructor super(message, throwable); } }
Constructor 1: CustomUncheckedException(String message)
This constructor allows you to create an exception with a custom error message. Calling super(message) invokes the constructors of all superclass(RuntimeException), which helps to create the complete stacktrace.
Constructor 2: CustomUncheckedException(Throwable cause)
Used to wrap another exception inside your custom exception. It calls super(cause) to pass the original exception (cause) to the superclass, which helps preserve the complete stack trace of the original error.
Constructor 3: CustomUncheckedException(String message, Throwable throwable)
Let you provide both a custom message and the original exception. This is the most informative way to throw an exception, providing full context.
Other ways to create an unchecked exception
Below are some of the other ways to create an unchecked exception:
- By extending the RuntimeException class
- By extending any subclass of RuntimeException (like NullPointerException, but that is considered as a bad practice)
If we extend any subclass of RuntimeException causes confusion. If you extend NullPointerException, it means your custom exception is behaving like a "null reference problem", which is misleading if the real problem is something else (like a login failure or payment error).