What is the difference between "Exception.Message" and "Exception.Tostring()"?

Asked

Viewed 256 times

4

I’ve read some answers regarding try-catch and on the Exceptionbut I still have doubt about the difference in Exception.Message and Exception.ToString():

  • What’s the difference between one and the other?
  • Which should I present to the user?
  • Which should I record in a bank of logs error for example?
  • See: http://stackoverflow.com/questions/2176707/exception-message-vs-exception-tostring

1 answer

5


ex. Message

Simply displays the basic error message.

ex Tostring.()?

It has much more information, there is everything that is important, including stack trace and internal exceptions (Inner Exception) except being observed, in addition to the Message. Culture-dependent data are presented in the current culture of the system.

Which should I present to the user?

What do you think is best for the context.

I would say to avoid the .ToString(). I find it better for other functions. It can be useful in debugging, eventually a log detailed development. It doesn’t help the user much. It’s a mistake that almost everyone makes.

Even the simple message might not be presented directly. There are cases to do this, but most of the time it will produce a better experience for the user to treat in a more personalized way even for each case.

If you have a centralised exception mechanism it is quite easy to treat the most common types more customarily (example). And the exceptions created for that system can be made by thinking of a better way.

Mounting a message using the various information available in the exception object will give the best result. Using something already mounted (.ToString()) is the easiest to do.

What should I write to an error log database for example?

I don’t know if any should be recorded directly. In a log simple and structured both can hinder. In a log that wants more information the message can help detect the problem. Only in a log more detailed is interesting log in the result of .ToString(). I think that kind of log is usually only useful while developing the application. In production it can be an exaggeration to produce this mass of information.

Of course it depends a little on purpose and even taste, according to the flow you prefer to work. There is no right or wrong in this.

Completion

Just don’t capture excess exceptions.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.