SonarQube major isuue raised 'Define and throw a dedicated exception instead of using a generic one.'?

Viewed 77

I need to throw RuntimeException for the KeycloakAuthenticationProvider . We are using SonarQube tool for code review purpose.

Here is the code

public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
      KeycloakAuthenticationProvider keycloakAuthenticationProvider = keycloakAuthenticationProvider();
      auth.authenticationProvider(keycloakAuthenticationProvider);
 }

Now, SonarQube raises an issue for the same , Now what should be replaced to fix the sonar violation.

1 Answers

The issue here is the throws Exception in your method signature:

public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception 

SonarQube informs you that instead of the generic Exception that is the basis of all exceptions, both checked and unchecked you shoud reference the actual (checked) exception that may be thrown by the methods that you are calling.

Usually, your IDE should already suggest a suitable Exception class.

Related