Unification of Exceptions Thrown from Java Methods
Annotate your methods with @UnitedThrows
annotation and every exception thrown out of the method will be an instance of the required type:
public class Thrower { @UnitedThrow public void do() throws IOException { throw new FileNotFoundException(); } }
Instead of FileNotFoundException
, an IllegalStateException
will be thrown which is the default for @UnitedThrow
.
Here is another example, but with a custom selected exception:
public class Thrower { @UnitedThrow(IOException.class) public void save() throws IOException, InterruptedException { throw new IllegalStateException(); } }
In this case, IllegalStateException
will not be thrown. Instead, IOException
will be used.
The mechanism is implemented with AOP/AspectJ. Read to know how to integrate it into your pom.xml
.