All TalkersCode Topics

Follow TalkersCode On Social Media

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

How To Throw Custom Exception In Java

Last Updated : Mar 11, 2024

How To Throw Custom Exception In Java

In this article we will show you the solution of how to throw custom exception in java, exceptions in Java cover the vast majority of common exceptions that are unavoidable in programming.

However, we are occasionally required to modify existing system exceptions or introduce us own exceptions to meet the needs of our use case.

Exceptions for consumers or customs are referred to as consumer or custom exceptions

In Java, we can start creating an exception by trying to extend the Exception class.

Almost everything general exception which is bound to occur in programming is covered by Java exceptions.

However, we must occasionally supplement such standard exceptions on our own.

Business logic exceptions are exceptions that are unique to core functionality and workflow. These assist app users and developers in determining the exact nature of the problem.

To catch and handle as a subset of existing Java exceptions. Exceptions in Java could be checked as well as unchecked. In the following sections, we'll go over every one of these scenarios.

Adapting Existing Java Exception - Assume you created a calc and the user attempts to allocate a number by O.

Now, during running time, Java will throw an Arithmetic exception, which will confuse this same user and cause your program to crash.

As a programmer, you will catch the preceding exception and throw a custom exception informing this very same user that they are attempting to div a number by zero, which is incorrect.

Step By Step Guide On How To Throw Custom Exception In Java :-

public class TalkersCodeException
{
  public static void main(String[] args)
  {
 Foo foo = new Foo();
 try
 {
   String bar = foo.getBar(0);
 }
 catch (AlsCustomException e)
 {
      e.printStackTrace();
 }
  }
}
  1. Make your own Exception. InvalidInputException is a class that extends Exception.
  2. A java.lang class.
  3. Make a Constructor Method that takes a String comment as an argument. Using a string as just an argument enables us to send a custom msg each moment we throw the exception.
  4. Call super(statement), which is a superclass's function Object() { [native code] } method with a statement as an input. With the exception, this will print with us personal note.

Conclusion :-

Adapting Existing Java Exception - Assume you created a calc and the user attempts to allocate a number by O.

Now, during running time, Java will throw an Arithmetic exception, which will confuse this same user and cause your program to crash.

As a programmer, you will catch the preceding exception and throw a custom exception informing this very same user that they are attempting to div a number by zero, which is incorrect.

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

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪