Too often, you see the following pattern in Java initiative programs
public Something myMethod() throws MyException { try { // may throw checked DbException: Connection c = openDataBase("connectionstring"); // ... other code throwing more checked exceptions ... } catch(DbException e) { throw new MyException(e); } return something; }
... or some other mechanism to "distinguish" one checked type of exception that is not declared in the method header to the one that is. Very often, this "try-catch-cast" -block must be included in every method of such a class.
I wonder how to implement an annotation that catches and transforms exceptions ?
The code used should look like this:
@ConvertException(MyException, DbException) public Something myMethod() throws MyException {
Of course, maybe you need to write instead of MyException.class or "MyException" . It should also be possible to link these annotations or list a few exceptions for conversion.
I assume that the wrapper function will be introduced in the annotation with a block of catching code that will call the original function. Then the annotation will only contain the time of the compiled time (and not the repetition of the execution time).
I am not saying that itโs wise to do this; itโs probably not because these annotations change the semantics of the program. It may be the academic question of โjust learningโ ...
source share