No, you don't "inherit" non-default constructors, you need to define the one taking a String in your class. Typically you use super(message) in your constructor to invoke your parent constructor. For example, like this:
public class MyException extends Exception {
public MyException(String message) {
super(message);
}
}
Answer from djna on Stack OverflowNo, you don't "inherit" non-default constructors, you need to define the one taking a String in your class. Typically you use super(message) in your constructor to invoke your parent constructor. For example, like this:
public class MyException extends Exception {
public MyException(String message) {
super(message);
}
}
A typical custom exception I'd define is something like this:
public class CustomException extends Exception {
public CustomException(String message) {
super(message);
}
public CustomException(String message, Throwable throwable) {
super(message, throwable);
}
}
I even create a template using Eclipse so I don't have to write all the stuff over and over again.
How do you structure your exception classes?
java.lang Exception hierarchy - why?
Was doing great in Java class until Exceptions. This one stupid thing is gonna make me fail
A Throwable other than Error or Exception?
Videos
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:
An AlertException class that we subclass and throw. We catch these at the top level and alert accordingly.
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!