Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a C# exception handler, can I find the parameters that were passed to the method that raised the exception? [duplicate]

Tags:

I'm writing an error handling module for a fairly complex system architected into layers. Sometimes our data layer throws obscure exceptions.

It would be really handy to log out the values of the parameters of the method that threw the exception.

I can reflect on the TargetSite property of the exception to find the method's parameter types and names, but I don't seem to be able to get the values... am I missing something?


Dupe

In a .net Exception how to get a stacktrace with argument values

like image 382
Jeremy McGee Avatar asked Nov 21 '08 16:11

Jeremy McGee


2 Answers

The built in framework ArgumentOutOfRangeException (which you should throw in a method if the incoming parameters are... out of range... has private field and public property for the method parameter that caused the error... When you create this exception, you pass the parameter value in the ctor...

throw new ArgumentOutOfRangeException(string parameterName, 
           object actualValue, string message);

For other exceptions, if you catch the exception in the method where it is thrown, and wrap it in a custom exception of your own, that has additional fields and properties for those method parameters

like image 56
Charles Bretana Avatar answered Sep 30 '22 19:09

Charles Bretana


In short ... no. See this question on capturing method state for some reasons.

like image 28
Rob Walker Avatar answered Sep 30 '22 18:09

Rob Walker