Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define an interface throwing a generic exception type?

I wanna define an interface, like

public interface Visitor <ArgType, ResultType, SelfDefinedException> {
     public ResultType visitProgram(Program prog, ArgType arg) throws SelfDefinedException;
     //...
}

during implementation, selfDefinedException varies. (selfDefinedException as a generic undefined for now) Is there a way to do this?

Thanks

like image 993
SkyOasis Avatar asked Mar 19 '12 22:03

SkyOasis


1 Answers

You just need to constrain the exception type to be suitable to be thrown. For example:

interface Visitor<ArgType, ResultType, ExceptionType extends Throwable> {
    ResultType visitProgram(String prog, ArgType arg) throws ExceptionType;
}

Or perhaps:

interface Visitor<ArgType, ResultType, ExceptionType extends Exception> {
    ResultType visitProgram(String prog, ArgType arg) throws ExceptionType;
}
like image 173
Jon Skeet Avatar answered Oct 09 '22 01:10

Jon Skeet