How To Create Custom Exception In Java
Last Updated : Mar 11, 2024
In this article we will show you the solution of how to create custom exception in java, in Java, we are able to derivate our own exception classes from the base Exception class.
A customized exception or consumer exception is one we create on our own. In essence, Java customized exceptions are employed to tailor the exception to user requirements.
The Error class, on the other hand, handles more serious problems in one’s Java program architecture and isn't handled inside the application code.
InternalError, AssertionError, OutOfMemoryError, and other errors in Java are examples.
We can generate our own exception and messages by using the custom exception.
Here, we've used the getMessage() method on the newly generated object to retrieve a string that we supplied to the function Object() { [native code] } of the object's superclass, the Exception class.
Nearly all of the common sorts of programming exceptions are covered by Java exceptions. But occasionally we have to make our own exceptions.
Following are a few of the reasons to use custom exceptions:
- To detect a subset of current Java exceptions and provide them particular treatment.
- Exceptions to business logic These are the business logic and process exceptions. Understanding the precise issue is helpful for both app users and developers.
We must extend an Exception class from the java.lang package in order to produce a unique exception.
One thing to keep in mind at this point would be that unchecked Exceptions all are subclasses of a RuntimeException class.
Later in this article, we'll go over did check and unchecked exceptions in greater detail. But first, so there is no confusion, let's look at how Errors, as well as Exceptions, differ in one‘s operation.
The exception hierarchy inside the programming java begins with the Throwable class, which also descends from of the Object class and is its direct subclass.
The exception is a class that represents all exceptions. This Throwable class is further subdivided into Error and Exception.
Step By Step Guide On How To Create Custom Exception In Java :-
class TalkersCodeException extends Exception { } public class TalkersCodeException2 { public static void main(String args[]) { try { throw new TalkersCodeException(); } catch (TalkersCodeException ex) { System.out.println("TalkersCode exception"); System.out.println(ex.getMessage()); } System.out.println("All Code"); } }
- First we develop class that utilizes custom exception TalkersCodeException.
- Then we build main function like public static void main.
- Next, we throw a user-defined exception object.
- Following that, talkerscode exceptions are caught.
- Next, we close the programme using the system.out.printIn.
Conclusion :-
In Java, we are able to derivate our own exception classes from the base Exception class.
A customized exception or consumer exception is one we create on our own.
In essence, Java customized exceptions are employed to tailor the exception to user requirements.
I hope this article on how to create custom exception in java helps you and the steps and method mentioned above are easy to follow and implement.