
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to create a custom unchecked exception in Java?
We can create the custom unchecked exception by extending the RuntimeException in Java.
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
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
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); } }
- Related Articles
- How to create a user defined exception (custom exception) in java?
- How can we create a custom exception in Java?
- How can we decide that custom exception should be checked or unchecked in java?
- Java Program to Handle Unchecked Exception
- Is it possible to create a custom exception in java without extending Exception class?
- While chaining, can we throw unchecked exception from a checked exception in java?
- C++ Program to Create Custom Exception
- Difference Between Checked and Unchecked Exception in Java
- Can we throw an Unchecked Exception from a static block in java?
- How to implement a custom Python Exception with custom message?
- Are the instances of Exception checked or unchecked exceptions in java?
- How to throw custom exception in Kotlin?
- How to create custom color maps in Java using OpenCV?
- How to handle the NumberFormatException (unchecked) in Java?
- How to handle the StringIndexOutOfBoundsException (unchecked) in Java?

Advertisements