How can I make my dynamic proxy throw checked exceptions?
I need a transparent wrapper for an interface which sometimes throws checked exceptions such as IOException
. Is it possible without 3rd party AOP or writing my own proxy? Modifying all 20 methods of the interface by hand is not an option either.
What you probably are looking for is this, as Konrad mentions above:
Source: https://web.archive.org/web/20120130204437/http://benpryor.com/blog/2006/08/15/java-dynamic-proxies-and-invocationtargetexception/
A dynamic proxy can throw checked exception if the exception is declared in the signature of the method of the interface it is proxying. From the Sun's Dynamic Proxy reference:
You can use a dynamic proxy. As long as the checked exceptions are part of the interface you can throw the checked exceptions from the invocation handler. Otherwise this is illegal and will create an UndeclaredThrowableException that wraps the thrown checked exception.
Output:
With an undeclared checked exception for interface A: