Java – should you report the message text of the exception?

Consider some code that can throw a check exception (an exception of type exception) Of course, your code catches exceptions Not only do you swallow this exception, your code will report to the user in some way through the user interface In the log file, or use the GUI pop-up window

Whether the text you report to the user contains abnormal message text That's throwable Getmessage() or throwable Text provided by getlocalizedmessage()?

I don't think so, but it seems that many people don't agree with me So what's wrong with me? My argument is as follows

>Create a message when an exception is thrown Therefore, it can only provide very low-level information at most, which may not be suitable for reporting to users. > Philosophically, using this message seems to oppose the whole exception, that is, separating the detection and startup of error handling (throwing part) from the completion processing and reporting (capturing part) Using this message means that the message must contribute to the report, which shifts the responsibility of the report to the location responsible only for detection and startup That is, I think the GetMessage () part of throwable design is an error. > Message not localized Despite its name, getlocalizedmessage () is not very good, because you may not know what locale you want to use until you catch an exception (report to go to the system log to read your English system administrator, or pop up in the window of French users?). > I heard that Java 7 has greatly improved the exception hierarchy for IOException, enabling you to handle different types of I / O errors in different catch clauses, making the GetMessage () text less important This means that even Java designers are uncomfortable with GetMessage ()

I'm not asking if report stack tracking is useful Stack traces are only useful for exceptions that suggest errors That is, for unchecked exceptions In this case, I think it is not only useful but also mandatory to provide low-level details of exception messages But my problem involves checking exceptions, such as file not found

Solution

No, exceptions should not be directly displayed to users in error messages. They are low-level technical details. Users almost always want something easier to understand, even if it does not provide as much information as stack traces!

Almost always, because of some situations (such as IDE), you can think that your users have enough technical ability to view stack traces; Indeed, in this case, they may prefer its "dumping" error message

However, I personally believe that stack traces should always be recorded where users can access them so that if they complain that "the program doesn't work", you can see how they are sent to your files

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
分享
二维码
< <上一篇
下一篇>>