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

2

I have already read some answers regarding try-catch and Exception s, but I still have doubts about the difference in Exception.Message and Exception.ToString() :

  • What's the difference between one and the other?
  • What should I submit to the user?
  • What should I write to an error bank logs for example?
asked by anonymous 08.08.2016 / 14:39

1 answer

3
  

ex.Message

Displays only the basic error message very simply.

  

ex.ToString ()?

It presents much more information, there is everything that is important, including stack trace and inner exceptions ( inner exception ) to the exception being observed, in addition to the Message . Culture-dependent data is displayed in the current system culture.

  

Which one should I present to the user?

What do you think is best for the context.

I would say to avoid .ToString() . I think it's better for other functions. It can be useful in debugging, possibly a detailed log in development. It does not help the user much.

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

If you have a centralized exception-support mechanism, it's easy to handle the more common more custom types ( example ). And the exceptions created for that system can be made thinking in a better way.

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

  

What should I write to a bank of error logs for example?

I do not know if it should be recorded directly. In a simple and structured log both can get in the way. In a log that wants more information the message can help detect the problem. Only in a more detailed log is interesting logar the result of .ToString() . I think this kind of log is usually 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 about it.

Conclusion

Just do not catch excess exceptions.

    
08.08.2016 / 14:55