1

I know the question doesn't make much sense so here is an example. I have a library where customers could construct sql query that could potentially take a long time. When it times out, sql reader throws SqlException. There could be consumers of this library listening specifically to SqlException.

Here, i want to communicate what the actual commandtext was back to the exception handler. SqlException is sealed and doesn't provide public constructor so I can instantiate a new SqlException nor can i instantiate derived class with custom messages like " Sql command executed: Select * from ... "

For now, i am throwing new Exception with original SqlException as inner exception but it could make consumers miss this exception.

Any help would be appreciated. Note that i am not asking if this is the right way to pass exception information. We could have used some sort of logging, pass back message or others but they had their share of problems so i am exploring this option.

Edit: Problem with using ex.Data is that the consumer has to know to check it. To give more information, my library is used in many places including legacy codes. I want consumers of my library to do the same thing it is doing today (no code change) but when they log Exception.ToString(), my additional message shows up there. My library and all of the consumers are internal and not shipping so logging Exception.ToString() is fine but it doesn't seem to expose Data.

user156144
  • 2,215
  • 4
  • 29
  • 40
  • Before you think of changing anything in an exception, you should make certain that the consumers of the exception will actually _use_ what you add. What will they do with the extra information? – John Saunders Apr 27 '14 at 01:00
  • this is core library to our entire internal system and most of the consumers retry on retriable sql exceptions. When retry doesn't work, some consumer choose to log the exception in event log, some choose to send email to the right alias and etc. I want all of consumers who are including "Ex.Tostring()" to get additional information i am putting in free, without code changes as i don't have access to all consumers and some of them are legacy code – user156144 Apr 27 '14 at 19:12
  • I understand why Exception.Message only has get accessor, but it would have been nice if there was another property, AdditionalMessage or something like that so that i can rethrow with custom message – user156144 Apr 27 '14 at 19:14

1 Answers1

1

Add your information to the Data property on the exception. The MSDN page has an example of what you're trying to do.

http://msdn.microsoft.com/en-us/library/system.exception.data(v=vs.110).aspx

Mike Parkhill
  • 5,511
  • 1
  • 28
  • 38
  • Thanks. Problem with Data is that the consumer has to know to check it. To give more information, my library is used in many places including legacy code. I want consumers of my library to do the same thing it is doing (no code change) but when they log Exception.ToString(), my additional message shows up there. My library and all of the consumers are internal and not shipping so logging Exception.ToString() is fine but it doesn't seem to expose Data. – user156144 Apr 27 '14 at 19:09
  • Hmm, in that case you might be able to get away with one of the back door ways to instantiate a class using reflection (http://stackoverflow.com/questions/1386962/how-to-throw-a-sqlexceptionneed-for-mocking - see @Sam Saffron's answer) or possibly using Activator. – Mike Parkhill Apr 28 '14 at 01:28