开发者

How do you extend the Java Exception class with a custom exception that takes a String parameter?

开发者 https://www.devze.com 2023-04-07 08:54 出处:网络
So I see you can extend the Exception class like so: public FooException(Exception e) { th开发者_JAVA百科is.initCause(e);

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);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消