If you are rethrowing an exception to the UI layer (so the user will see it) is it always wise to rethrow a specific exception (Eg argumentnullexception)?
The problem I've seen with this is that it is too much technical information to he user. All I need to know is the problem and the cause, hiding any details and not even mention the word 开发者_开发技巧exception.
I saw the rethrowing of a specific exception in some code I was reviewing and was wondering if this is really recommended?
Thanks
What are you doing with the exception once it reaches the UI layer?
If you're just letting the system exception handler catch it and shut down your program, then the more specific the better, because the user won't be seeing it anyway.
If you're catching it in the UI layer and telling the user what went wrong, then the more specific the better, and the UI layer can worry about "prettying it up" so that the user isn't bamboozled by too much technical information.
I don't think any exceptions should ever be thrown to the UI tier. It ought to be caught, logged, and translated into something meaningful for a user.
Are you logging the specific exception? If the answer is yes, I would say rethrow a general exception. I assume that you have a try/catch
block in your UI tier that does something that makes sense, like display a message box with the error message in the exception.
精彩评论