Define and throw a dedicated exception instead of using a generic one | Community
Skip to main content
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Asutosh_Jena_

Hi @vinuu123 

 

You should not throw generic exceptions. You should be subclassing Exception and then throwing your subclass, so that the type of the exception actually provides information about what is going on, allowing clients of the function to catch and treat it appropriately.

 

Example:

Here Exceptio is generic in nature and you should not be using it. Instead of that you should use the specific category type i.e., either "LoginException", "RepositoryException", depending upon the nature of exception. Or you can define your own exception and use here.

 

try {
// Some code
} catch (final Exception e) {
LOG.error("***** :: Exception :: ***** {}", e.getMessage());
}

 

Thanks!

1 reply

Asutosh_Jena_
Community Advisor
Asutosh_Jena_Community AdvisorAccepted solution
Community Advisor
February 23, 2022

Hi @vinuu123 

 

You should not throw generic exceptions. You should be subclassing Exception and then throwing your subclass, so that the type of the exception actually provides information about what is going on, allowing clients of the function to catch and treat it appropriately.

 

Example:

Here Exceptio is generic in nature and you should not be using it. Instead of that you should use the specific category type i.e., either "LoginException", "RepositoryException", depending upon the nature of exception. Or you can define your own exception and use here.

 

try {
// Some code
} catch (final Exception e) {
LOG.error("***** :: Exception :: ***** {}", e.getMessage());
}

 

Thanks!