Trap error in sub or function, return sub or function name?

  • Thread starter Thread starter Tim Frawley
  • Start date Start date
T

Tim Frawley

When I trap an error in a sub or function is there a programatic way
to determine the sub or function name for error reporting?

Private Sub cmdOk_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles
cmdOk.Click
Try

Error occurrs

Catch Ex as exception

WriteToEventLog(FormName & "." & SubOrFunctionName,
ex.tostring)

End Try

End Sub

This would really save me a lot of typing the names in here. Form
Name is easy but the Sub or Function name has me stumped.

Tim
 
well you can check ex.StackTrace, but as far as I remember it doesnt give
you too many details when you are running your app in Release mode..
give it a try though
 
Tim,

First add an import to the top of your form...

Imports System.Reflection

....then use the following code in your procedure to get the current class
(form) name and/or method name...

Dim strClassName As String
Dim strMethodName As String

With MethodBase.GetCurrentMethod()
strClassName = .ReflectedType.ToString()
strMethodName = .Name
End With


Hope this helps.

Gary
 
That does work, at least in Debug mode. I didn't get a chance to test
it as a Release as the next response to my post provided an easier
method.

Thank you for your help!
 
Hi Tim,

Not to you, just in general.

I have seen a lot of stupid solutions, but this one gets a ++ in my eyes.
Your code can not be set in another function, so it has to be in every sub
or function.
I did not check it, but as an answer is your answer of course ok.

However to replace this simple code
\\\\
Private Sub cmdOk_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles
cmdOk.Click
Try
Error occurrs
Catch Ex as exception
WriteToEventLog(FormName & "cmdOK_Click." & ex.tostring)
End Try
///
By this
\\\\
Private Sub cmdOk_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdOk.Click
Dim strClassName As String
Dim strMethodName As String
With MethodBase.GetCurrentMethod()
strClassName = .ReflectedType.ToString()
strMethodName = .Name
End With
Try
Error occurrs
Catch Ex as exception
WriteToEventLog(FormName & strMethodName & ex.tostring)
End Try
///

Is in my opinion only good if you are paid per row of code.

:-)

Just my thought.

Cor
 
Tim,
Generally I go with the ex.StackTrace as MrPolite suggested. (specifically I
use ex.ToString)

In Debug builds StackTrace gives method names, along with file names & line
numbers.

In Release builds StackTrace gives just method names. (no file names & line
numbers).

The advantage of the stack trace obviously is that you have the entire trace
of where the exception is coming from.


However!!! I would not include the above code in each event handler. I would
include it once in the Application.ThreadException event handler.

Something like:
Private Sub cmdOk_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles
cmdOk.Click

Error occurrs

End Sub


Public Shared Sub Main()
AddHandler Application.ThreadException, AddressOf OnThreadException
Application.Run(New MainForm)
End Sub

Private Sub OnThreadException(sender As Object, _
ByVal e As System.Threading.ThreadExceptionEventArgs)
Try

WriteToEventLog(ex.ToString())
Catch ex As Exception
' Don't want to end the app here! ;-)
End Try
End Sub



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.

Hope this helps
Jay
 
Back
Top