473,803 Members | 3,461 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 = MyCustomExcepti on)
FillFromDataBas e(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 2861
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.Diagnost ics.StackFrame frame = new
System.Diagnost ics.StackFrame( 1);
MethodBase method = frame.GetMethod ();
ParameterInfo[] parameters = frame.GetMethod ().GetParameter s();
bool showValues = parameters.Leng th == parameterValues .Length;
Console.WriteLi ne(method.Name) ;
Console.WriteLi ne("Parameters: ");
for (int p = 0; p < parameters.Leng th; ++p)
{
Console.Write(" \t" + parameters[p].Name);
if (showValues)
{
Console.Write(" \t" + parameterValues[p]);
}
Console.WriteLi ne();
}
}
Then any time you call HandleException , just add the method parameters
to the end:

public int DivideNumbers(i nt 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.co m> wrote in message
news:OC******** *****@TK2MSFTNG P11.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.Diagnost ics.StackFrame frame = new
System.Diagnost ics.StackFrame( 1);
MethodBase method = frame.GetMethod ();
ParameterInfo[] parameters = frame.GetMethod ().GetParameter s();
bool showValues = parameters.Leng th == parameterValues .Length;
Console.WriteLi ne(method.Name) ;
Console.WriteLi ne("Parameters: ");
for (int p = 0; p < parameters.Leng th; ++p)
{
Console.Write(" \t" + parameters[p].Name);
if (showValues)
{
Console.Write(" \t" + parameterValues[p]);
}
Console.WriteLi ne();
}
}
Then any time you call HandleException , just add the method parameters to
the end:

public int DivideNumbers(i nt 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
3772
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
13577
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 parameter names & data types. Is there a way to get the parameter v a l u e s as well ??? ( I know ... this is indeed weird, but .. don't ask :D )
2
5846
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: Parameter has no default value. But it works fine in Access when I run the query from there and just hit
1
14570
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 paramter value from ParameterInfo class. is there a way I can read real parameter values? Thanks!!
2
1869
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 method. Also enableViewStateMac is made false in the destination page. Still I cannot get some changed values. The scenario is such - 1. I have page 1 with a hidden variable named,"myHid" whose value is set to "Text1" 2. When a submit happens on...
2
6492
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 signature for method), is this possible without exception-wrapping the guts of each method? I can see how to get parameter values out of MethodInfo, but can't see how to do it without doing something like adding 7 lines to each method, taking...
6
1973
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 entry fields on the form housing perhaps 600-700 characters between the lot of them. Why might Access 97 need to err on such a simple operation? SQL: INSERT INTO tblAdmin (TowCompany, TowCoAddr, TowCoCity, TowCoState, TowCoZip, TowCoCounty,...
2
2494
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 value of one of the parameters that were passed to the method. Is this possible? Even if I have to use PInvoke to do so? I can currently find the parameter TYPEs using reflection but I can not figure out to obtain the values. Thanks,
0
1285
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 value=??? how do i give the access fields values to oracle parameters and pass it sqlComm.Parameters.Add(New OracleParameter("name", OracleType.VarChar)).Value = value sqlComm.Parameters.Item("name").Direction =...
0
9703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10550
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10295
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9125
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7604
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5501
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5633
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.