473,320 Members | 2,012 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,320 software developers and data experts.

Catch Exception Block

Hi,
I have created a StringBuilder (Log) in my application that appends all the
errors and exceptions in my application. I have included Catch blocks at each
sensitive location in my functions.
I was wondering if there is a way that i can get the name of the function in
which the exception has occured like
try{}
catch(Exception e)
{
Log.Append(e.FunctionName + "--" + e.Message);
}

How can get the name of the function using e Args. I don't want to hard code
the FunctionName in each catch block.

Thanks!

With Regards
Sunny
Nov 16 '05 #1
3 3198
Sunny,

You can take a look at the StackTrace property of the Exception instance
that you catch. StackFrame instance at the bottom should be the stack frame
that threw the exception. You can then get the MethodInfo instance of the
method by calling the GetMethodInfo method on the StackFrame instance.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Sunny" <Su***@discussions.microsoft.com> wrote in message
news:3E**********************************@microsof t.com...
Hi,
I have created a StringBuilder (Log) in my application that appends all
the
errors and exceptions in my application. I have included Catch blocks at
each
sensitive location in my functions.
I was wondering if there is a way that i can get the name of the function
in
which the exception has occured like
try{}
catch(Exception e)
{
Log.Append(e.FunctionName + "--" + e.Message);
}

How can get the name of the function using e Args. I don't want to hard
code
the FunctionName in each catch block.

Thanks!

With Regards
Sunny

Nov 16 '05 #2
Hi,

Use Exception.StackTrace

Take also a look at Exception.InnerException, it can contain also some info
you may be interested in.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Sunny" <Su***@discussions.microsoft.com> wrote in message
news:3E**********************************@microsof t.com...
Hi,
I have created a StringBuilder (Log) in my application that appends all the errors and exceptions in my application. I have included Catch blocks at each sensitive location in my functions.
I was wondering if there is a way that i can get the name of the function in which the exception has occured like
try{}
catch(Exception e)
{
Log.Append(e.FunctionName + "--" + e.Message);
}

How can get the name of the function using e Args. I don't want to hard code the FunctionName in each catch block.

Thanks!

With Regards
Sunny

Nov 16 '05 #3
Sunny,
In addition to the other comments, instead of (in addition to) using a
try/catch in each of your sensitive routines, consider having a global
exception handler, this way you only need to log the exception in a single
location.

Depending on the type of application you are creating, .NET has three
different global exception handlers.

For ASP.NET look at:
System.Web.HttpApplication.Error event
Normally placed in your Global.asax file.

For console applications look at:
System.AppDomain.UnhandledException event
Use AddHandler in your Sub Main.

For Windows Forms look at:
System.Windows.Forms.Application.ThreadException event
Use AddHandler in your Sub Main.

It can be beneficial to combine the above global handlers in your app, as
well as wrap your Sub Main in a try catch itself.

There is an article in the June 2004 MSDN Magazine that shows how to
implement the global exception handling in .NET that explains why & when you
use multiple of the above handlers...

http://msdn.microsoft.com/msdnmag/is...T/default.aspx

For example: In my Windows Forms apps I would have a handler attached to the
Application.ThreadException event, plus a Try/Catch in my Main. The
Try/Catch in Main only catches exceptions if the constructor of the MainForm
raises an exception, the Application.ThreadException handler will catch all
uncaught exceptions from any form/control event handlers.

Hope this helps
Jay
"Sunny" <Su***@discussions.microsoft.com> wrote in message
news:3E**********************************@microsof t.com...
Hi,
I have created a StringBuilder (Log) in my application that appends all
the
errors and exceptions in my application. I have included Catch blocks at
each
sensitive location in my functions.
I was wondering if there is a way that i can get the name of the function
in
which the exception has occured like
try{}
catch(Exception e)
{
Log.Append(e.FunctionName + "--" + e.Message);
}

How can get the name of the function using e Args. I don't want to hard
code
the FunctionName in each catch block.

Thanks!

With Regards
Sunny

Nov 16 '05 #4

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

Similar topics

6
by: Erik Cruz | last post by:
Hi. I have read several articles recommending avoid to raise exceptions when possible, since exceptions are expensive to the system. Removing code from Try... Catch blocks can help performance?...
8
by: Z D | last post by:
Hi, I was wondering what's the point of "finally" is in a try..catch..finally block? Isn't it the same to put the code that would be in the "finally" section right after the try/catch block?...
3
by: Travis | last post by:
I am trying to prevent an exception from bubbling up from one nested Try/Catch Block to its "Parent" Try/Catch Block. Here is some example code: try{ try{ //Non-application killing code that...
11
by: Pohihihi | last post by:
I was wondering what is the ill effect of using try catch in the code, both nested and simple big one. e.g. try { \\ whole app code goes here } catch (Exception ee) {}
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
20
by: Woody Splawn | last post by:
In a message yesterday titled Try Catch Question I got numerous responses. Thank you to all. After all the smoke clears I guess the question is where to place the Return True statement in the...
5
by: PasalicZaharije | last post by:
Hallo, few days ago I see ctor like this: Ctor() try : v1(0) { // some code } catch(...) { // some code }
4
by: cj | last post by:
my old code Try Dim sw As New System.io.StreamWriter(fileName, True) sw.WriteLine(strToWrite) sw.Close() Catch End Try my new code
16
by: HillBilly | last post by:
This is freaking me out. I'm using Membership and trying to determine if the database is online. The GetConnectionString( ) method returns a connection string as expected but not when used in the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.