Method that gets current location in code?

G

Guest

Hi,

I'm newish to C# .Net. I'm doing some error handling in my ASP.net pages.

When handling the error in the catch block, I want to pass through the
namespace of where the error occured.. something like
"application.page.method" - I could hardcode this for every ASP.net page, but
I'm sure there must be something in .Net that gives me this for free.

Does anyone know of something I could use?

Cheers!
 
P

Patrice

AFAIK Exception.ToString has this info. The stack trace could perhaps be
avialable separately...


Patrice
 
S

Scott Allen

When a program executes, each method is given a "stack frame" as an
area for temporary storage. Local variables and a return address to
the caller are often stored in the stack frame.

When a method calls a second method, a new stack frame is generated
for the call. When the second method returns that new stack frame can
be cleaned up - it's just temporary storage for one method call.

When there is an exception you can not only tell where you are, but
where you have been by looking at a 'stack trace'. A stack trace
examines the active stack frames on the thread to see how you arrived
at this point.

You can start digging into stack frames yourself by looking at the
StackTrace property of an exception.
 
B

Brock Allen

Look into the Exception.TargetSite. Exception.TargetSite.Name is the name
of the method. Exception.TargetSite.DeclaringType.FullName is the name of
the containing class.

OTOH, calling Exception.ToString() will print all this out for you (and more).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top