Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Uses of Custom Exception
3.
How to create a Custom Exception?
4.
Example
5.
Output
6.
Explanation
7.
FAQs
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

User-defined Custom Exception

Author Ayushi Poddar
1 upvote
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

Sometimes during the execution of a program, an issue(run time error) is encountered, which is referred to as an exception, and when it occurs, the program in execution is abruptly stopped, and the code after the line which generated the exception is not executed. 

When we create our own exception, it is known as a user-defined exception or custom exception, which is possible as Java allows it since they are derived classes of Exception. A custom exception is made using the ‘throw’ keyword.

Also see,  Swap Function in Java

Uses of Custom Exception

Most of the usual exceptions are covered in Java during programming, but for specific purposes, we might need to create custom exceptions. Some of the uses of custom exceptions are as follows:

  • It allows you to add methods and attributes that are not part of the standard exception.
  • To catch and provide specific treatment to a subset of existing Java exceptions.
  • For exceptions related to business logic and its workflow. Very useful for developers to understand the exact problem.
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

How to create a Custom Exception?

We need to extend the exception class in java.lang package to create our custom exception. With the following example, we will get a better understanding of it.

Example

// A class having the custom exception
 
class My_Exception extends Exception {
    public My_Exception(String s)
    {
        // Calling constructor of parent Exception
        super(s);
    }
}
public class Main {
    public static void main(String args[])
    {
        try {
            // Throws an object of custom exception
            throw new My_Exception("Coding Ninjas Studio");
        }
// catches the exception
        catch (My_Exception ex) {
            System.out.println("Caught");
 
            // Display the message from My_Exception object
            System.out.println(ex.getMessage());
        }
    }
}

Output

Caught
Coding Ninjas Studio


Practice by yourself on java online compiler.

Explanation

In the example above, My_Exception requires a string as its argument so to the constructor of the parent class Exception, the string is passed using the getMessage() object. The call to super is not compulsory as the constructor of the parent class can also be called without a parameter.

Also read, Duck Number in Java and Hashcode Method in Java

FAQs

  1. What is a custom exception?
    When we create our own exception, it is known as a user-defined exception or custom exception, which is possible as Java allows it since they are basically derived classes of Exception.
  2. What are the uses of a custom exception?
    Most of the usual exceptions are covered in Java during programming. Still, we might need to create custom exceptions for specific purposes as it allows you to add methods and attributes that are not part of the standard exception and to catch and provide specific treatment to a subset of existing Java exceptions.
  3. How to create a custom exception?
    We need to extend the exception class in java.lang package to create our custom exception.

Conclusion

In this article, we have extensively discussed the following things:

  1. What is a custom exception?
  2. How is a custom exception created?
  3. Uses of custom exception.

We hope that this blog has helped you enhance your knowledge regarding User-defined Custom exceptions and if you would like to learn more, check out our articles here. Do upvote our blog to help other ninjas grow. Happy Coding!

 

Previous article
Null Pointer Exception in Java
Next article
Exception Handling with Method Overriding in Java
Live masterclass