🌐
TutorialsPoint
tutorialspoint.com › how-to-print-custom-message-instead-of-errorstacktrace-in-java
How to print custom message instead of ErrorStackTrace in java?
import java.util.Scanner; class MyException extends Exception{ public MyException(String msg){ super(msg); } } public class PrintingExceptionMessage { public static void main(String args[]) throws Exception { String msg = "This is my custom exception"; Scanner sc = new Scanner(System.in); System.out.println("Enter first number: "); int a = sc.nextInt(); System.out.println("Enter second number: "); int b = sc.nextInt(); try { int c = a/b; System.out.println("The result is: "+c); }catch(ArithmeticException e) { MyException exce = new MyException(msg); throw exce; } } } Enter first number: 14 Enter second number: 0 Exception in thread "main" july_set3.MyException: This is my custom exception at july_set3.PrintingExceptionMessage.main(PrintingExceptionMessage.java:23)
🌐
Baeldung
baeldung.com › home › java › core java › create a custom exception in java
Create a Custom Exception in Java | Baeldung
May 11, 2024 - We’ve created and used a custom exception, so the user can now know what the exact exception is. Is this enough? We are consequently losing the root cause of the exception. To fix this, we can also add a java.lang.Throwable parameter to the constructor. This way, we can pass the root exception to the method call: public IncorrectFileNameException(String errorMessage, Throwable err) { super(errorMessage, err); } Now the IncorrectFileNameException is used along with the root cause of the exception:
🌐
Alvin Alexander
alvinalexander.com › java › java-custom-exception-create-throw-exception
Java: How to create and throw a custom exception | alvinalexander.com
As you can see, all you need to do to throw your custom exception is (1) create a new instance of the exception (new AlsCustomException("Anything but zero ...")), and then (2) throw that exception with the throw keyword. With those two pieces in place, we'll create a "driver" class with a main ...
🌐
Rollbar
rollbar.com › home › how to throw exceptions in java
How to Throw Exceptions in Java | Rollbar
2 weeks ago - Let's start with the basics of throwing exceptions then work our way up to when and how to create your own. Throwing an exception is as simple as using the "throw" statement. You then specify the Exception object you wish to throw.
🌐
JetBrains
blog.jetbrains.com › idea › 2024 › 03 › easy-hacks-how-to-throw-java-exceptions
Easy Hacks: How to Throw Java Exceptions | The IntelliJ IDEA Blog
March 12, 2024 - Let’s create a custom exception that represents a banking overdraft. For example, when a user attempts to withdraw more money from their account than the available balance, this exception will be thrown. In its simplest form, the OverdraftException could look like this: public class OverdraftException extends Exception { public OverdraftException(String message) { super(message); } }
Find elsewhere
🌐
Stackify
stackify.com › java-custom-exceptions
Implement Custom Exceptions in Java: Why, When and How
May 1, 2023 - /** * The MyBusinessException wraps all checked standard Java exception and enriches them with a custom error code. * You can use this code to retrieve localized error messages and to link to our online documentation. * * @author TJanssen */ public class MyBusinessException extends Exception { ... } Quite often, your code catches a standard exception before you throw your custom exception.
🌐
GeeksforGeeks
geeksforgeeks.org › java › user-defined-custom-exception-in-java
User-Defined Custom Exception in Java - GeeksforGeeks
Create a new class that extends Exception (for checked exceptions) or RuntimeException (for unchecked exceptions). Provide constructors to initialize the exception with custom messages...
Published   August 14, 2025
🌐
Hero Vired
herovired.com › home › learning-hub › topics › custom-exceptions-in-java
How to Create Custom Exceptions in Java - Hero Vired
This blog will guide you through creating a custom exception in Java. We’ll cover the steps and differences between checked and unchecked exceptions and provide practical examples. Imagine you’re building a banking app and need to check for a specific error that the standard exceptions don’t cover: A user tries to withdraw more money than they have. A standard exception might not provide the best feedback. But, a custom exception can give a clear, understandable error message.
🌐
Medium
medium.com › @nweligalla › creating-custom-exceptions-in-java-ea77a61fcaf4
Creating Custom Exceptions in Java | by Nayana Weligalla | Medium
January 31, 2024 - This amount is sent to the parent class’s constructor with a concatenated string using super(). Because you’re overriding the default Java constructor with one that takes an amount parameter, you can also create another no-args constructor and use it to throw an exception directly. if you want you can call the super() without any parameters. You don’t have to only extend the Exception class; you can also extend the RuntimeException for your custom exception.
🌐
Vultr Docs
docs.vultr.com › java › examples › create-custom-exception
Java Program to Create custom exception | Vultr Docs
December 16, 2024 - The method declares that it might throw this type of exception by using the throws keyword. Sometimes, you might want to include more context or functionality in your custom exception class.
Top answer
1 of 5
7

Is it bad practice to throw multiple custom exceptions in Java?

No. It is good practice.

The only situation where multiple exceptions might be a (slightly) bad idea is if there is absolutely no possibility (ever!) of catching individual exceptions; i.e. the fine-grained exceptions won't serve any functional purpose. However, my opinion is that if you can make that assertion, then there is probably something wrong with the way that you / your team is using Java. And the counter-argument is that sensible use of multiple custom exceptions will help you to document the APIs ... even if you are never going to do more than catch-log-and-bail-out at runtime.

This is not to say that lots of custom exceptions will always be good:

  • If you go overboard and create a separate exception for everything, then you are probably adding unnecessary complexity. (In a lot of cases, different exception messages are sufficient.)

  • If you don't have a sensible inheritance hierarchy for your custom exceptions, then you may end up regretting it. (A well-designed hierarchy allows you to catch "classes" of exceptions, or declare methods as throwing them. It can make your code simpler.)

2 of 5
7

Is it bad practice to throw multiple custom exceptions in Java?

Generally speaking: No. Why should it?

As with everything: Abstraction is your friend.

Is it necessarry to have a CustomerNotFound Exception and a ProductNotFound Exception? Or are your requirements just a more abstract NotFoundException? The context could help to determine, what was missing. Having different exceptions for the sake of having them is nonsense.

Is it necessary, each layer of your application having custom exceptions? Exceptions are a way to report, that an intended action failed due to some reason.

  • Say, you have a controller which asks the service-layer to retrieve data, which in turn asks the DA-layer to read values from the DB. The resultset is empty. The service gets the empty resultset and throws a NotFoundException the service communicates, the failure of the action due to a missing result.

  • Say, the controller needs the service to do the payrolls for employees. And the service is asked to do the payroll for the employee with ID 123456, and in turn asks a service to retrieve the employee - but no emloyee could be found.

There are two ways to deal with that:

1) You throw a NotFound exception in the DA-Layer, catch it in the payroll-service and rethrow a PayrollServiceException wrapping the NotFoundException with the message Exmployee could not be found

2) You throw a NotFound exception in the DA-Layer and do not catch it in the payroll service and catch it instead a layer above.

I would go for (2), since in (1) the information, that the action failed because of a missing employee is redundant.

🌐
DataCamp
datacamp.com › doc › java › throw
throw Keyword in Java: Usage & Examples
This example demonstrates how to throw a custom exception. The validate method throws a CustomException if the number is less than or equal to zero, and the main method catches and handles it. Use Meaningful Messages: Always provide meaningful and descriptive messages when throwing exceptions to make debugging easier.
🌐
CodeJava
codejava.net › java-core › exception › how-to-create-custom-exceptions-in-java
How to create custom exceptions in Java
It’s a common practice for catching a built-in exception and re-throwing it via a custom exception. To do so, let add a new constructor to our custom exception class. This constructor takes two parameters: the detail message and the cause of the exception.
🌐
DZone
dzone.com › data engineering › databases › implementing custom exceptions in java
Implementing Custom Exceptions in Java
November 13, 2017 - /** * The MyBusinessException wraps all checked standard Java exception and enriches them with a custom error code. * You can use this code to retrieve localized error messages and to link to our online documentation. * * @author TJanssen */ public class MyBusinessException extends Exception { ... } Quite often, your code catches a standard exception before you throw your custom exception.
🌐
Medium
medium.com › javarevisited › how-to-throw-exceptions-in-java-using-throw-throws-keywords-throwing-exceptions-7082007f6462
How To Throw Exceptions In Java Using throw, throws Keywords | Throwing Exceptions | by Mouad Oumous | Javarevisited | Medium
February 22, 2024 - However, it is possible for your program to throw an exception explicitly, using the throw statement. The throw keyword is used to explicitly throw a single exception. We specify the exception object which is to be thrown.