473,387 Members | 1,530 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.

Find Name of Function

How can I find the name of the current function being executed.

For example, lets say I have the following function

void Blah(int? y){
int z;
try {
z = y;
}
catch(Exception ex){
throw ex
}
}
Which will throw an exception if y is null(Nullable).
I am writing a class to log any exceptions to a SQL database, a column
indicating the name of the current function where the exception was
generated would be very handy.
So instead of throwing the exception, I would log it, and handle the
exception gracefully.

Not to sure if this can be done, on binaries not compiled without
debugging symbols. So does anyone care to enlighten me?
Sep 27 '07 #1
11 1817
Mick Walker wrote:
How can I find the name of the current function being executed.

For example, lets say I have the following function

void Blah(int? y){
int z;
try {
z = y;
}
catch(Exception ex){
throw ex
}
}
Which will throw an exception if y is null(Nullable).
I am writing a class to log any exceptions to a SQL database, a column
indicating the name of the current function where the exception was
generated would be very handy.
So instead of throwing the exception, I would log it, and handle the
exception gracefully.

Not to sure if this can be done, on binaries not compiled without
debugging symbols. So does anyone care to enlighten me?
My bad...

void Blah(int? y){
int z;
try {
z = (int)y;
}
catch(Exception ex){
throw ex
}
}
Sep 27 '07 #2
Try using Google search and you will find many suggestions:

<http://www.google.com/search?source=...23+get+name+of
+current+function>

Sep 27 '07 #3
Roger Stewart wrote:
Try using Google search and you will find many suggestions:

<http://www.google.com/search?source=...23+get+name+of
+current+function>
Did that before posting here. I actually found
MethodBase.GetCurrentMethod().Name.ToString()

The reason I didn't mention it was because I wanted to find the general
consensus on how to do it.
Sep 27 '07 #4
It appears System.Diagnostics.StackTrace and
System.Diagnostics.StackFrame classes are your best bet.

Sep 27 '07 #5
On Sep 27, 10:11 am, Mick Walker <materiali...@privacy.netwrote:
Roger Stewart wrote:
Try using Google search and you will find many suggestions:
<http://www.google.com/search?source=...23+get+name+of
+current+function>

Did that before posting here. I actually found
MethodBase.GetCurrentMethod().Name.ToString()

The reason I didn't mention it was because I wanted to find the general
consensus on how to do it.
If you are logging exceptions, why not just log the StackTrace member
of the exception as part of your logging method? If there is no pdb
file, you'll be missing the filename and line numbers...

Here's why I would rather have the ENTIRE stack trace rather than just
the current method...imagine that your method is called from 10
different places, but only ONE of those callers is passing in a bad
value. Without knowing the CALLING function, what good would logging
your method name be? Sure, you'd know that Blah failed. But I'd
rather know that "Foo" caused it to fail...

Sep 27 '07 #6
There is no need to start using Reflection or Diagnostics to find where an
error occurred. The exception has a .Source attribute that can be queried as
the exception rides up the stack.

I would not, as Roger has mentioned, catch exceptions at every level. You
will end up bogging down your application this way, with no real benefit. In
general, unhandled exceptions should be caught as close to the UI as
possible. Other exception handlers should be employed only when there is
some way to actually handle the exception. This is not a firm rule, of
course, but catching simply to throw is not a good strategy.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

*************************************************
| Think outside the box!
|
*************************************************
"Mick Walker" <ma**********@privacy.netwrote in message
news:5m************@mid.individual.net...
How can I find the name of the current function being executed.

For example, lets say I have the following function

void Blah(int? y){
int z;
try {
z = y;
}
catch(Exception ex){
throw ex
}
}
Which will throw an exception if y is null(Nullable).
I am writing a class to log any exceptions to a SQL database, a column
indicating the name of the current function where the exception was
generated would be very handy.
So instead of throwing the exception, I would log it, and handle the
exception gracefully.

Not to sure if this can be done, on binaries not compiled without
debugging symbols. So does anyone care to enlighten me?

Sep 27 '07 #7
"Mick Walker" <ma**********@privacy.netwrote in message
news:5m************@mid.individual.net...
How can I find the name of the current function being executed.

For example, lets say I have the following function

void Blah(int? y){
int z;
try {
z = y;
}
catch(Exception ex){
throw ex
}
}
Which will throw an exception if y is null(Nullable).
I am writing a class to log any exceptions to a SQL database, a column
indicating the name of the current function where the exception was
generated would be very handy.
So instead of throwing the exception, I would log it, and handle the
exception gracefully.
Be careful of how you do this. I see many developers catching exceptions all
over the place, when they should let the exceptions bubble up to a higher
level, to be handled there.

Also, the Exception.Source property usually has good information about where
the exception happened.

I'd suggest that you either log ex.ToString(), or else serialize the
exception object (assuming it is serializable).
--
--------------------------------------------------------------------------------
John Saunders | MVP - Windows Server System - Connected System Developer

Sep 27 '07 #8
"Doug Semler" <do********@gmail.comwrote in message
news:11*********************@k79g2000hse.googlegro ups.com...
On Sep 27, 10:11 am, Mick Walker <materiali...@privacy.netwrote:
>Roger Stewart wrote:
Try using Google search and you will find many suggestions:
<http://www.google.com/search?source=...23+get+name+of
+current+function>

Did that before posting here. I actually found
MethodBase.GetCurrentMethod().Name.ToString()

The reason I didn't mention it was because I wanted to find the general
consensus on how to do it.

If you are logging exceptions, why not just log the StackTrace member
of the exception as part of your logging method? If there is no pdb
file, you'll be missing the filename and line numbers...

Here's why I would rather have the ENTIRE stack trace rather than just
the current method...imagine that your method is called from 10
different places, but only ONE of those callers is passing in a bad
value. Without knowing the CALLING function, what good would logging
your method name be? Sure, you'd know that Blah failed. But I'd
rather know that "Foo" caused it to fail...
Does anyone feel that logging Exception.ToString() is not enough? I suppose
that one might want to put the .Source into a separate column to enable
grouping...
--
--------------------------------------------------------------------------------
John Saunders | MVP - Windows Server System - Connected System Developer

Sep 27 '07 #9
The OP did not mention using obfuscation but I will throw this out
there... You are also opening up another can of worms if you
obfuscate. A stack trace or Exception.Source will be of no value.

Sep 27 '07 #10
Mick Walker wrote:
How can I find the name of the current function being executed.

For example, lets say I have the following function

void Blah(int? y){
int z;
try {
z = y;
}
catch(Exception ex){
throw ex
}
}
Which will throw an exception if y is null(Nullable).
I am writing a class to log any exceptions to a SQL database, a column
indicating the name of the current function where the exception was
generated would be very handy.
So instead of throwing the exception, I would log it, and handle the
exception gracefully.

Not to sure if this can be done, on binaries not compiled without
debugging symbols. So does anyone care to enlighten me?
Thanks for all your suggestions.

Basically this information will be used in a testing environment. Based
on the type of exception, the appropriate developer will be notified.

It will never be used within a live web application. Its simply a tool
to aid during the testing phase.
Sep 28 '07 #11
On Sep 27, 6:52 pm, Roger Stewart <rstewart27...@gmail.comwrote:
The OP did not mention using obfuscation but I will throw this out
there... You are also opening up another can of worms if you
obfuscate. A stack trace or Exception.Source will be of no value.
It should be. When you obfuscate, good tools will produce a mapping
file (which of course you don't distribute) saying which original name
mapped to which output name.

I remember writing a little webapp when we used a Java obfuscator -
you'd paste the stack trace reported by the customer into the tool,
select the build number, and it would give the unobfuscated stack
trace.

Jon

Sep 28 '07 #12

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

Similar topics

6
by: Peter Hansen | last post by:
Greetings. Im trying to write a program that can be run from the command line. If I want to search for example after a file with the ending .pdf, I should be able to write in the command line:...
4
by: John Bailo | last post by:
I have created several aliases to members using the CREATE ALIAS command, but I lost track of them. Now I want to see a catalog of all the aliases mapped to all members in a FILE. Is there a...
5
by: Tim Eliot | last post by:
Just wondering if anyone has hit the following issue and how you might have sorted it out. I am using the command: DoCmd.TransferText acExportMerge, , stDataSource, stFileName, True after...
5
by: Filips Benoit | last post by:
Dear All, I'm looking for a function that does the same as the find-action in the code-window. Thanks, Filip
2
by: John Regan | last post by:
Hello All I am trying to find the owner of a file or folder on our network (Windows 2000 Server) using VB.Net and/or API. so I can search for Folders that don't follow our company's specified...
5
by: Joerg Battermann | last post by:
Hello there, I have a custom type defined via Public Class Requirement Public IDNumber As Integer Public Name As String Public Description As String Public VersionPlanAttributes As New _
27
by: one man army | last post by:
Hi All- I am new to PHP. I found FAQTS and the php manual. I am trying this sequence, but getting 'no zip string found:'... PHP Version 4.4.0 $doc = new DomDocument; $res =...
2
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
4
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.