I have extended NLog to log all items in the Exception.Data collection. So, when I catch an SqlException, in my data access code, I like to add a few items to the Exception.Data dictionary to provide better logging. I don’t always want to log from within the catch, but instead, let the exception bubble up and be handled later. So I write something like this:
Try
...
Catch exception As Exception
exception.Data.Add("SP", StoreProcedureNameCost)
exception.Data.Add("Connection", myConnection.ConnectionString)
Throw
End Try
Does calling just Throw instead of Throw exception still have have all of my added Data items?
Yes, It will preserve the changes made to Data property. The fundamental difference between
throwandthrow exis that the stack trace does not change with callingthrowwhereas it gets changed withthrow ex.