Reputation: 1459
I'm using SpringFramework 4.3.2 and SonarQube v5.6.1 with FindBugs and getting the following violation:
Unchecked/unconfirmed cast from Exception to java.rmi.RemoteException
this is the original code:
@ExceptionHandler(RemoteException.class)
public ResponseEntity<ClientErrorInformation> resourceRemoteExceptionHandler(HttpServletRequest req, Exception e){
RemoteException re = (RemoteException) e;
...
return new ResponseEntity<ClientErrorInformation>(new ClientErrorInformation(null, "Internal server error", req.getRequestURI(), false,null), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
When using the spring @ExceptionHandler annotation ,it means that only the specified kind of exception will be set as argument to the method. in this case RemoteException.
Can you please tell me what's wrong with that code? or i can ignore that kind of violations in this code.
Thanks
Upvotes: 1
Views: 1037
Reputation: 131436
When using the spring @ExceptionHandler annotation ,it means that only the specified kind of exception will be set as argument to the method. in this case RemoteException.
Can you please tell me what's wrong with that code? or i can ignore that kind of violations in this code.
Spring knows what the caught exception is of RemoteException
type but FindBugs doesn't rely on Spring implementation to detect potential rule violation. From FindBugs view, it sees only a cast without check. That's why it indicates a potential rule violation.
In your case, you don't need to ignore the violation since Spring allows to specify the exception type as you wish.
org.springframework.web.bind.annotation
Annotation Type ExceptionHandler
Handler methods which are annotated with this annotation are allowed to have very flexible signatures. They may have parameters of the following types, in arbitrary order:
An exception argument: declared as a general Exception or as a more specific exception...
So, as @chrylis suggested, you should declare your method like that to avoid the cast :
public ResponseEntity<ClientErrorInformation> resourceRemoteExceptionHandler(HttpServletRequest req, RemoteException e)
Upvotes: 2