Java – exception handling mode

See a common pattern where the error code associated with the exception is stored as a static final int When an exception is created to be thrown, it is constructed as one of these codes and an error message

The alternative seems to be – declare a class for each exception error condition (although the associated exception is eliminated from a common base class)

Is there a middle position? What is the recommended method?

Solution

This is a good question I believe it is definitely a middle ground

Error codes are essential for displaying errors in quality checks and for customers to return customer support to developers

I personally do not recommend error codes for handling errors programmatically. I will suggest a new class for each category of errors, but definitely not every error Java has done a decent job. Let's start with exceptions, such as IOException, illegalargumentexception, unsupported operation exception, etc I often throw and catch these in my code

If you have an exception of a new class and your code should respond programmatically, you should definitely create a new class for this and extend the corresponding parent class For example, userregistrationexception or productexception

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>