I would at the very least define your own exception base type so that every exception that your code throws is an instance of that type. Depending on how many systems/modules/libraries your systems consists of, perhaps you need more than one base type. If you're interested in differences in meaning between various instances of your base type exception then I recommend creating different subclasses, but I would try to make those meaningful in the sense that they should convey what sort of error they're signalling. We now have a filter layer that filters out alerts that match a regex This you should not be doing. The information you need to make these decisions should be communicated either via structured fields (enums, booleans, etc) or the exception type. An AlertException class that we subclass and throw This I would definitely not do. The whole point of exceptions is that in your code when something bad happens you can package up information about the problem and throw it up the stack so that higher-level code which knows what the context is can handle the problem. Because what the correct error handling is depends on context. If you have a class like this then basically every time you throw an exception you have to make this alert/not alert decision, but you don't want those decisions spread all over the codebase. You also don't necessarily know everywhere in the codebase whether this is alertable or not. Instead, make the code communicate what sort of error has happened, and then have the decisions at higher levels. Personally, I think it sounds odd to do alerting on individual exceptions instead of using metrics and defining alerts on those. Note that this is all generic advice, because you said nothing about what sort of application this is. Answer from larsga on reddit.com
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Exception.html
Exception (Java Platform SE 8 )
October 20, 2025 - The class Exception and any subclasses that are not also subclasses of RuntimeException are checked exceptions. Checked exceptions need to be declared in a method or constructor's throws clause if they can be thrown by the execution of the method or constructor and propagate outside the method ...
🌐
Tutorialspoint
tutorialspoint.com › java › java_exceptions.htm
Java - Exceptions
Errors are typically ignored in your code because you can rarely do anything about an error. For example, if a stack overflow occurs, an error will arise. They are also ignored at the time of compilation. All exception classes are subtypes of the java.lang.Exception class.
🌐
Reddit
reddit.com › r/java › how do you structure your exception classes?
r/java on Reddit: How do you structure your exception classes?
January 17, 2024 -

On my team, we mostly use the built-in Java exceptions with a top level try-catch that logs and alerts us.

You probably see where this is going.. we get alerted for every single exception, and many of these are not actionable by us (user errors).

We now have a filter layer that filters out alerts that match a regex, so we aren’t alerted for everything now but it’s still not ideal.

I want a way to distinguish between exceptions we need to be alerted on and exceptions that can just be warnings.

I see 2 main approaches:

  1. An AlertException class that we subclass and throw. We catch these at the top level and alert accordingly.

  2. Some sort of Alertable marker interface that we throw and do an instance of check on. One variant is the interface could also have a method like “responseType” that returns an enum with values like: Alert Oncall, Warn, Alert user

I’m leaning towards approach 2 but would love to hear your ideas! Thanks!

Top answer
1 of 22
55
Is this a Spring Web app? If so there is a built-in annotation called @ControllerAdvice. You can annotate a global exception handler class with that and you can handle custom exceptions using that. For the custom exceptions you'll want to subclass the built-in Exception class.
2 of 22
24
I would at the very least define your own exception base type so that every exception that your code throws is an instance of that type. Depending on how many systems/modules/libraries your systems consists of, perhaps you need more than one base type. If you're interested in differences in meaning between various instances of your base type exception then I recommend creating different subclasses, but I would try to make those meaningful in the sense that they should convey what sort of error they're signalling. We now have a filter layer that filters out alerts that match a regex This you should not be doing. The information you need to make these decisions should be communicated either via structured fields (enums, booleans, etc) or the exception type. An AlertException class that we subclass and throw This I would definitely not do. The whole point of exceptions is that in your code when something bad happens you can package up information about the problem and throw it up the stack so that higher-level code which knows what the context is can handle the problem. Because what the correct error handling is depends on context. If you have a class like this then basically every time you throw an exception you have to make this alert/not alert decision, but you don't want those decisions spread all over the codebase. You also don't necessarily know everywhere in the codebase whether this is alertable or not. Instead, make the code communicate what sort of error has happened, and then have the decisions at higher levels. Personally, I think it sounds odd to do alerting on individual exceptions instead of using metrics and defining alerts on those. Note that this is all generic advice, because you said nothing about what sort of application this is.
🌐
Rollbar
rollbar.com › home › java exceptions hierarchy explained
Java Exceptions Hierarchy Explained | Rollbar
May 15, 2025 - Java exceptions can be of several types and all exception types are organized in a fundamental hierarchy. Understanding this hierarchy is crucial for implementing robust error handling strategies in production. The class at the top of the exception class hierarchy is the Throwable class, which is a direct subclass of the Object class.
🌐
Stackify
stackify.com › types-of-exceptions-java
Types of Exceptions in Java - Stackify
March 14, 2024 - Now let us explore different types of exceptions in Java. The parent class of all the exception classes is the java.lang.Exception class.
🌐
GeeksforGeeks
geeksforgeeks.org › java › exceptions-in-java
Java Exception Handling - GeeksforGeeks
In Java, exception handling is a mechanism to handle runtime errors, allowing the normal flow of a program to continue. Exceptions are events that occur during program execution that disrupt the normal flow of instructions.
Published   December 23, 2016
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › Exception.html
Exception (Java Platform SE 7 )
The class Exception and any subclasses that are not also subclasses of RuntimeException are checked exceptions. Checked exceptions need to be declared in a method or constructor's throws clause if they can be thrown by the execution of the method or constructor and propagate outside the method ...
Find elsewhere
🌐
Coding Shuttle
codingshuttle.com › java-programming-handbook › class-exception
Class Exception in Java
April 9, 2025 - Java allows developers to create their own exception classes by extending Exception. When built-in exceptions do not represent a specific error scenario in your application.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.exception
Exception Class (Java.Lang) | Microsoft Learn
The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch. [Android.Runtime.Register("java/lang/Exception", DoNotGenerateAcw=true)] public class Exception : ...
🌐
Raygun
raygun.com › blog › java-exceptions-terminology
Java exceptions: Common terminology with examples · Raygun Blog
October 25, 2022 - Exception throwing in Java happens with the throw statement. ArithmeticException, ArrayIndexOutOfBoundsException, NullPointerException, and IOException in our examples were all thrown automatically by the Java platform.
🌐
DigitalOcean
digitalocean.com › community › tutorials › exception-handling-in-java
Exception Handling in Java | DigitalOcean
October 6, 2022 - RuntimeException is the parent class of all Runtime Exceptions. If we are throwing any Runtime Exception in a method, it’s not required to specify them in the method signature throws clause. Runtime exceptions can be avoided with better programming. Java Exception and all of its subclasses ...
🌐
Educative
educative.io › answers › what-are-different-types-of-exceptions-in-java
What are different types of exceptions in Java?
They are used to handle errors and other exceptional events in programs written in the Java programming language. ... Checked exceptions: These are the exceptions that are checked by the compiler at compile time.
🌐
Javatpoint
javatpoint.com › exception-class-in-java
Exception Class in Java - Javatpoint
Exception Class in Java with java tutorial, features, history, variables, object, programs, operators, oops concept, array, string, map, math, methods, examples etc.
🌐
Florida State University
cs.fsu.edu › ~myers › cop3331 › notes › javaexcept.html
Exception Handling in Java
Catching an exception - Exception handlers (blocks of code) are created to handle the different expected exception types. The appropriate handler catches the thrown exception and performs the code in the block · In a Java method claim an exception with the keyword throws.
🌐
Runestone Academy
runestone.academy › ns › books › published › javajavajava › exception-hierarchy.html
Java’s Exception Hierarchy
In many cases leaving the handling of such exceptions up to Java may be the best course of action, as we will see Section 10.5. ... An unchecked exception—one belonging to some subclass of RunTimeException—does not have to be caught within your program. ... The java.lang.Exception class itself is very simple, consisting of just two constructor methods (Figure 10.3.7).
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › lang › Exception.html
Exception (Java SE 11 & JDK 11 )
October 20, 2025 - The class Exception and any subclasses that are not also subclasses of RuntimeException are checked exceptions. Checked exceptions need to be declared in a method or constructor's throws clause if they can be thrown by the execution of the method or constructor and propagate outside the method ...
🌐
Oracle
docs.oracle.com › javase › 6 › docs › api › java › lang › Exception.html
Exception (Java Platform SE 6)
The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch. Since: JDK1.0 · See Also: Error, Serialized Form · public Exception() Constructs a new exception with null as its detail message.
🌐
Airbrake
blog.airbrake.io › blog › java-exception-handling › the-java-exception-class-hierarchy
The Java Exception Class Hierarchy
July 13, 2022 - The goal here is just to give a quick overview of these categories, as much more detailed looks into specific exceptions will come in future articles. AssertionError - Thrown when an assertion has failed. LinkageError - Thrown when a class dependency has some form of incompatibility, due to changes made after compilation. ThreadDeath - Thrown when the (now deprecated) Thread.stop() method is invoked. VirtualMachineError - Thrown when something goes wrong with the Java Virtual Machine, such as running out of resources.
🌐
Tutorialspoint
tutorialspoint.com › java › java_builtin_exceptions.htm
Java - Built-in Exceptions
package com.tutorialspoint; public class ExcepTest { public static void main(String args[]) { int b = 0; int c = 1/b; System.out.println("c :" + c); } } Exception in thread "main" java.lang.ArithmeticException: / by zero at com.tutorialspoint.ExcepTest.main(ExcepTest.java:8)