Reputation: 2295
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.
Upvotes: 1
Views: 106
Reputation: 5551
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
Upvotes: 1