How do you extend the Java Exception class with a custom exception that takes a String parameter?
So I see you can extend the Exception class like so:
public FooException(Exception e)
{
th开发者_JAVA百科is.initCause(e);
}
But what if you want to pass not an exception to FooException's constructor - but a String:
public FooException(String message)
{
// what do i do here?
}
What would the body of the Constructor look like in that case?
Exception
also has a String
constructor.
class FooException extends Exception {
public FooException(String message) {
super(message);
}
}
Erm ... that's not how you extend an exception. Create your own subclass which already takes a string as the argument.
To extend a class you should do something like this:
public class FooException extends Exception {
FooException(String message) {
super(message);
}
}
精彩评论