DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 3: Understanding Janus
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 2: Understanding Neo4j
  • How to Introduce a New API Quickly Using Micronaut
  • Introducing Graph Concepts in Java With Eclipse JNoSQL

Trending

  • Navigating Change Management: A Guide for Engineers
  • Memory-Optimized Tables: Implementation Strategies for SQL Server
  • AI Speaks for the World... But Whose Humanity Does It Learn From?
  • Simpler Data Transfer Objects With Java Records
  1. DZone
  2. Coding
  3. Java
  4. Custom Exceptions in Java

Custom Exceptions in Java

In this article, create a custom exception class in Java.

By 
Shatakshi Dixit user avatar
Shatakshi Dixit
·
May. 11, 20 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
14.1K Views

Join the DZone community and get the full member experience.

Join For Free

We can create custom exception class in Java very easily. It can either be a checked or unchecked exception.

Let's create a simple example to check creation of custom exceptions in Java.

We will learn

  1. How to create a custom exception class
  2. Throw the custom Java exception
  3. Catch the custom exception
  4. Check the output

Java Custom Exception:

The below class is a simple java class to create custom exception.

Java
 




xxxxxxxxxx
1
13


 
1
class CustomExceptionEx extends Exception
2
3
{
4
5
  public CustomExceptionEx(String string)
6
7
  {
8
    //calling the super
9
    super(string);
10
11
  }
12
13
}



Steps to Create Exception:

  • Create a java class
  • Extend the Exception class
  • call the super()

There exists other constructors in the Exception class as well. This is the basic example to create a custom exception. It is the most used way.

Throw Java Custom Exception

In the above example, we have created a custom exception - CustomExceptionEx.

Now, let's throw this exception in our java code example.

Java
 




x
18


 
1
class Sample
2
{
3
  public String checkNum(int num)
4
  throws CustomExceptionEx
5
  {
6
    if (i == 0)
7
    {
8
      //throwing the exception
9
      throw new CustomExceptionEx("Number is zero");
10
    }
11
    else
12
    {
13
      return "Number is non zero";
14
    }
15
  }
16
}



Steps to Throw Exception:

  1. Create instance of exception CustomExceptionEx
  2. Throw exception using throw keyword
  3. Declare the exception in the method using throws keyword

Test Java Custom Exception

Now, create a main class and test the created java custom exception.

Java
 




xxxxxxxxxx
1
17


 
1
public class MainClass
2
{
3
  public static void main(String[] args)
4
  {
5
   
6
    Sample object = new Sample();
7
    
8
    try
9
    {
10
      //calling the method
11
      String bar = object.checkNum(0);
12
    }
13
    catch (CustomExceptionEx e)
14
    {
15
      e.printStackTrace();
16
    }
17
  }
18
}



In the above class, we are calling the  checkNum  method of  Sample  class, which is throwing the custom created exception.

It was simple, right? If you have any questions, please post in the comments.

Thanks for reading!

Java (programming language)

Published at DZone with permission of Shatakshi Dixit. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 3: Understanding Janus
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 2: Understanding Neo4j
  • How to Introduce a New API Quickly Using Micronaut
  • Introducing Graph Concepts in Java With Eclipse JNoSQL

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!