473,387 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Get access to Parameter values off the Stack

Hi all

I would like to get the values of Parameters passed to a method from a
centralized exception handling routine. At the moment, the exception is
passed to the handler, and I can get the stack and find out which methods
are in it and what parameters those methods take, but I would really like to
know the values of the parameters, something like below:

HandleException(Exception Ex = MyCustomException)
FillFromDataBase(Integer pkValue = 1)
Main()

If I have a look in the VS.NET designer when debugging, I can see the values
of the parameters. It would be really useful, for logging purposes, to know
the values of the parameters in the exception handler rather than putting
code in each method.

I've looked around for this and saw some-one post a response to a similar
question saying that this is not possible unless you use the Debugging API,
is this true? If it is true, does anyone know of any resources or have any
code samples of how it's done? I can't believe that I'm the first person who
wants to do this, so if it can be done then I'm sure some-one has done it
already!!

Thank you.

Kind Regards,
Steve.
Dec 9 '05 #1
2 2844
I don't know of a way to do it automatically, but you could get the same
effect with slightly more work. Just change your HandleException
method to take the parameter values as a params array (sorry, I do not
know the VB.NET equivalent off hand, but I'm pretty sure it exists - if
not, just use a regular object array):
private void HandleException(Exception e, params object[] parameterValues)
{
System.Diagnostics.StackFrame frame = new
System.Diagnostics.StackFrame(1);
MethodBase method = frame.GetMethod();
ParameterInfo[] parameters = frame.GetMethod().GetParameters();
bool showValues = parameters.Length == parameterValues.Length;
Console.WriteLine(method.Name);
Console.WriteLine("Parameters:");
for (int p = 0; p < parameters.Length; ++p)
{
Console.Write("\t" + parameters[p].Name);
if (showValues)
{
Console.Write("\t" + parameterValues[p]);
}
Console.WriteLine();
}
}
Then any time you call HandleException, just add the method parameters
to the end:

public int DivideNumbers(int x, int y)
{
int answer = 0;
try
{
answer = x / y;
}
catch(Exception e){
HandleException(e, x, y);
}
return answer;
}

Not a perfect solution, but it works.
Joshua Flanagan
http://flimflan.com/blog
Dec 9 '05 #2
Hi Joshua

Thank you for your reply.

I was hoping to get what I want done without having to add code in each of
my methods, but I guess I'm not going to have much choice! Also, my
HandleException call is made in the 'top-level' methods, if I receive an
Exception in nested methods I just make the Throw (Ex) call to pass it back
up the chain, so there would be no way to pass through the values of any
Parameters in that method without changing the way I handle exceptions :o(

I guess I'll just wait a bit to see if anyone else comes up with a solution.
I haven't moved to 2.0 yet, we're doing that in Feb 2006, maybe there's
something new that can help me. If not then my parameter values will be lost
forever.....

Kind Regards,
Steve.

"Joshua Flanagan" <jo**@msnews.com> wrote in message
news:OC*************@TK2MSFTNGP11.phx.gbl...
I don't know of a way to do it automatically, but you could get the same
effect with slightly more work. Just change your HandleException method to
take the parameter values as a params array (sorry, I do not know the
VB.NET equivalent off hand, but I'm pretty sure it exists - if not, just
use a regular object array):
private void HandleException(Exception e, params object[] parameterValues)
{
System.Diagnostics.StackFrame frame = new
System.Diagnostics.StackFrame(1);
MethodBase method = frame.GetMethod();
ParameterInfo[] parameters = frame.GetMethod().GetParameters();
bool showValues = parameters.Length == parameterValues.Length;
Console.WriteLine(method.Name);
Console.WriteLine("Parameters:");
for (int p = 0; p < parameters.Length; ++p)
{
Console.Write("\t" + parameters[p].Name);
if (showValues)
{
Console.Write("\t" + parameterValues[p]);
}
Console.WriteLine();
}
}
Then any time you call HandleException, just add the method parameters to
the end:

public int DivideNumbers(int x, int y)
{
int answer = 0;
try
{
answer = x / y;
}
catch(Exception e){
HandleException(e, x, y);
}
return answer;
}

Not a perfect solution, but it works.
Joshua Flanagan
http://flimflan.com/blog

Dec 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Marcin | last post by:
Hello! Is there any method to detect parameters values passed to called method? For example: public Guid ApplicationLogin(string userName, string password, int dbId)
3
by: Angelos Karantzalis | last post by:
Hi y'all .. I hope this is an interesting one ... Within an executing method, I can call MethodBase.GetCurrentMethod() and retrieve info about the method that's currently running, including...
2
by: The Plankmeister | last post by:
Hi... I have a query which I'm accessing through PHP as a stored procedure. However, I need to be able not to pass a couple of parameters in certain situations. When I do this, I get an error: ...
1
by: Hardy Wang | last post by:
Hi all: I have a logging class, which is used to log errors automatically. With StackFrame.GetMethod().GetParameters() function, I am able to read all parameter information except the real...
2
by: P K | last post by:
I am using server.transfer for a website being developed in vs.net 2005 And I need to get the posted values after server.transfer. For this I set the second parameter "true" in the transfer...
2
by: news.microsoft.com | last post by:
Hi all. If I wanted to write something so that, when an exception was thrown, and the stack unwound, the stack trace was captured with the values of the parameters (instead of just the parameter...
6
by: MLH | last post by:
I have a query (SQL below) that operates on values entered by users into an unbound form to append a record to tblAdmin. I do not under- stand the basis for the error. There are some 17 or so data...
2
by: ME | last post by:
How would one obtain the parameter VALUES of a method that has already run? I can find the method using the StackTrace and StackFrame classes but once I find the method I would like to obtain the...
0
by: riyap | last post by:
i understood the oracle procedure but only question is after executing the access db query, how do i assign those values(each field values) to the parameters iam passing into oracle procedure i.e...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.