Error Codes in Exceptions Against Hierarchy Exceptions

Do you think it is normal to use error codes in an exception to indicate the type of error? Please take a look at this code:

public class MyException extends Exception { public static final String ERROR_CODE_INVALID_NAME = ""; public static final String ERROR_CODE_INVALID_ID = ""; ... private String errorCode; public MyException(String message, String errorCode) { super(message); this.errorCode = errorCode; } public String getErrorCode() { return errorCode; } } 

I know that it is better to use enum instead of Strings in this example, but I'm really concerned about the concept of error codes. Do you think the exceptions here would be better? I can not find any authoritative source that says that error codes under the exception are anti-patterns. thanks.

+8
java exception anti-patterns error-code
source share
5 answers

If you want to react differently (in code) depending on what caused the exception (invalid name or invalid identifier), I would suggest different exceptions.

If not, then you do not even need the getErrorCode() method, you can simply add an error code to the exception message, and the exception will provide you with all the necessary information for debugging.

+2
source share

Error codes are useful if

  • you cannot display a complete error message (dishwasher display)
  • the code must be processed internally (some logic is triggered if a certain code appears or the server sends an error code to the client while the client is responsible for the message)
  • We have a great guide and the user can use the code to get comprehensive information.
  • The user does not need to know what is happening, but needs to contact the seller

So, in most cases, I do not see the added value in the error codes. I prefer the hierarchy of exceptions, or at least a clear error message, which is really useful if it is in the log file (2 years after exiting the programmer's program).

If you have requirements for error codes, the solution is not bad. Consider collecting all error codes in a central repository (properties file) so that you can easily exchange the bundle:

 myexception.ERROR_CODE_INVALID_NAME=text or number myexception.ERROR_CODE_INVALID_ID=text or number 
+8
source share

From my experience, exception codes are mainly used as an informational message to the user.

I have not even seen that someone is trying to parse a general exception message in order to react differently, it depends on the error code, usually this is done through an exception hierarchy.

On the other hand, it would be difficult to create a new exception subclass for each specific case, and then use exception codes.
For example, if for the user code it does not take into account why the transaction failed, it should refuse it in any case, but for the end user it is important why this happened (incorrect parameters, connection to the database or other).

So, to summarize, if you expect different ways of handling different situations, it is better to use different types of exceptions, but if you should handle several problems the same way, but only notify the user about a specific reason, it is easier to use exception codes.

+3
source share

I usually use a combination of both.

You need to share your exceptions and make a constructive decision.

For example, you can use parameters such as the source of the exception, type, effect, and processing to classify your exception. If exceptions belong to the same category, use the error codes inside. Use a hierarchy to exclude falling into different categories.

If you selected the Processing exception of an important parameter exception, you can choose between two parameters depending on how you want to handle them:

  • Use error codes if you want to catch all types in one catch block and handle them in a common way.
  • Use a hierarchy if you want to catch a specific type at a time and process them accordingly.
+2
source share

Effectively creating a stacktrace of a complex exception hierarchy is very expensive in terms of both memory and time, so if you create a complex custom exception hierarchy for something that you can solve with 3-4 static error codes ... I would prefer the error code option. In general, I prefer to work with Runtime exceptions (not checked in the method signature), that checked exceptions caught the effective approach - this is a bit outdated IMO.

+1
source share

All Articles