I have a wrapper responsible for logging operations, named OperationWrapper. Its structure is simple and as follows:
public void runOperation(Operation o) throws Exception{
logOperationStarted();
o.execute();
logOperationFinished();
}
Since the "o" operation can throw an exception, the logOperationFinished()
method will not always be called and thus logging does not operate correctly.
Also, various components invoking the runOperation()
method handle these exceptions .
Having wanted to ensure that logOperationFinished()
will always run, I implemented the following structure:
public void runOperation(Operation o) throws Exception{
logOperationStarted();
try{
o.execute();
}
catch(Exception e){
throw e;
}
finally{
logOperationFinished();
}
}
Now logOperationFinished()
does always run, but I am getting a warning from IntelliJ:
Caught exception is immediately rethrown
Reports any catch block where the caught exception is immediately rethrown, without performing any action on it. Such catch blocks are unnecessary or lack error handling.
To me it seems the IntelliJ is not taking the finally block into account when issuing this warning.
Am I doing something wrong or is there a better way of achieving this?
Thanks.