<p>I've created a custom exception for a very specific problem that can go wrong. I receive data from another system, and I raise the exception if it bombs while trying to parse that data. In my custom exception, I added a field called "ResponseData", so I can track exactly what my code couldn't handle.</p>

<p>In custom exceptions such as this one, should that extra response data go into the exception "message"? If it goes there, the message could be huge. I kind of want it there because I'm using Elmah, and that's how I can get at that data.</p>

<p>So the question is either:
- How can I get Elmah to record extra information from a field in a custom exception
OR
- Should extra exception details go into the "message" property?</p>

[{'answer_id': 48785, 'author': 'Danimal', 'author_id': 2757, 'author_profile': 'https://Stackoverflow.com/users/2757', 'pm_score': 0, 'selected': False, 'text': "<p>I don't understand the question -- you're extending System.Exception, and you already added the Elmah field. That's where it belongs -- as a public property of the exception itself.</p>\n"}, {'answer_id': 48792, 'author': 'Sören Kuklau', 'author_id': 1600, 'author_profile': 'https://Stackoverflow.com/users/1600', 'pm_score': 4, 'selected': True, 'text': '<p>You shouldn\'t fill <code>.Message</code> with debug information, but rather with a concise, helpful piece of text.</p>\n<p><a href="http://msdn.microsoft.com/en-us/library/system.exception.message.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/system.exception.message.aspx</a></p>\n<blockquote>\n<p><strong>The text of Message should completely describe the error and should, when possible, explain how to correct it.</strong> The value of the Message property is included in the information returned by ToString.</p>\n<p>The Message property is set only when creating an Exception. If no message was supplied to the constructor for the current instance, the system supplies a default message that is formatted using the current system culture.</p>\n<p>[..]</p>\n<p>Notes to Inheritors:</p>\n<p>The Message property is overridden in classes that require control over message content or format. Application code typically accesses this property when it needs to display information about an exception that has been caught.</p>\n<p>The error message should be localized.</p>\n</blockquote>\n<p>Response data does not qualify as a description.</p>\n<p>Not being familiar with elmah, I can\'t tell you how to extend the <code>Exception</code> class while using it. Does elmah implement its own subclass to <code>Exception</code>? Or an interface? Can you subclass it yourself?</p>\n'}, {'answer_id': 48801, 'author': 'George Mauer', 'author_id': 5056, 'author_profile': 'https://Stackoverflow.com/users/5056', 'pm_score': 1, 'selected': False, 'text': "<p>I don't fully understand the question but you seem to be asking what to do with additional exception data, if that is not your question feel free to ignore this.</p>\n\n<p>I think an important question to ask is what exactly is the exception message for?  It is not for knowing where the exception came from, the stack trace is for that; it is not to encapsulate an exception in a more general one, that should be done with the InnerException field; in the case where your exception is only raised from a particular place in your code it isn't even for describing what kind of error you had - thats what the type of the exception is for.</p>\n\n<p>Generally I use the message field to provide simple, human-readable tips that a programmer that is not me, seeing this error for the first time can use to gain an understanding of the underlying system.  I consider the message field to be appropriate for a short (one sentence) explanation, a hint as to how this error is frequently raised, or a reference to further reading.</p>\n\n<p>So, as far as I understand your question, I think that the best way to store this 'additional information' that is received from another system is as an InnerException.  I don't know Elmah, but if it's worth its salt it will check for InnerExceptions and store them.</p>\n"}, {'answer_id': 48802, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>Elmah is a http module that records unhandled exceptions.</p>\n\n<p>I guess it's just a limitation of Elmah, since it doesn't store custom fields. I guess I'll have to ask those guys. I have the extra field in there for the response data, but Elmah does not store it.</p>\n"}, {'answer_id': 155606, 'author': 'Atif Aziz', 'author_id': 6682, 'author_profile': 'https://Stackoverflow.com/users/6682', 'pm_score': 4, 'selected': False, 'text': '<blockquote>\n  <p>In custom exceptions such as this one,\n  should that extra response data go\n  into the exception "message"?</p>\n</blockquote>\n\n<p>No, as <a href="https://stackoverflow.com/questions/48773/adding-extra-information-to-a-custom-exception#48792">Sören already pointed out</a>. However, your exception type could override <code>ToString</code> and sensibly add the response data information there. This is a perfectly normal practice followed by many of the exception types in the BCL (Base Class Library) so you will not find yourself swimming against the tide. For example, have a look at the <a href="http://tinyurl.com/rotor-filenotfound-tostring" rel="nofollow noreferrer">System.IO.FileNotFoundException.ToString</a> implementation in <a href="http://research.microsoft.com/sscli/" rel="nofollow noreferrer">SSCLI (Rotor)</a>:</p>\n\n<pre><code>public override String ToString()\n{\n    String s = GetType().FullName + ": " + Message;\n\n    if (_fileName != null &amp;&amp; _fileName.Length != 0)\n        s += Environment.NewLine + String.Format(Environment.GetResourceString("IO.FileName_Name"), _fileName);\n\n    if (InnerException != null)\n        s = s + " ---&gt; " + InnerException.ToString();\n\n    if (StackTrace != null)\n        s += Environment.NewLine + StackTrace;\n\n    try\n    {\n        if(FusionLog!=null)\n        {\n            if (s==null)\n                s=" ";\n            s+=Environment.NewLine;\n            s+=Environment.NewLine;\n            s+="Fusion log follows: ";\n            s+=Environment.NewLine;\n            s+=FusionLog;\n        }\n    }\n    catch(SecurityException)\n    {\n\n    }\n    return s;\n}\n</code></pre>\n\n<p>As you can see, it appends the content of <a href="http://msdn.microsoft.com/en-us/library/system.io.filenotfoundexception.fusionlog.aspx" rel="nofollow noreferrer">FusionLog</a> property, which represent <em>extra</em> information in case of assembly load failures. </p>\n\n<blockquote>\n  <p>How can I get Elmah to record extra\n  information from a field in a custom\n  exception</p>\n</blockquote>\n\n<p><a href="http://code.google.com/p/elmah/" rel="nofollow noreferrer">ELMAH</a> stores the result of calling <code>ToString</code> on an exception as the details of the error so if you have <code>ToString</code> implemented as prescribed, the information would get logged without further work. The only issue is that the logged detail will be <em>unstructured</em> text.</p>\n'}, {'answer_id': 155651, 'author': 'Gabriel Isenberg', 'author_id': 1473493, 'author_profile': 'https://Stackoverflow.com/users/1473493', 'pm_score': 3, 'selected': False, 'text': '<p>The Exception class contains a dictionary (named Data, I believe) that you can use to associate custom data with a vanilla exception. </p>\n'}]
['https://Stackoverflow.com/questions/48773', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']