How to not throw a generically specified exception?

  • A+
Category:Languages

I created a "producer" interface (to be used with method references, respectively to be easily mocked for unit tests):

@FunctionalInterface public interface Factory<R, T, X extends Throwable> {     public R newInstanceFor(T t) throws X; } 

which I created like that, as my first use case actually had to throw some checked WhateverException.

Now, my second use case ... doesn't have an X to throw.

The best I could come up with to make the compiler happy is:

Factory<SomeResultClass, SomeParameterClass, RuntimeException> factory; 

That compiles, and does what I need, but still ugly. Is there a way to keep that single interface, but not provide an X when declaring specific instances?

 


You cannot do that in Java. The only way is to create a sub interface.

public interface DefaultExceptionFactory<R, T>         extends Factory<R, T, RuntimeException> 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: