Return method name on exception

Hi all

I have Try Catches all through my code, but if and when an exception occurs, I'd like to return the class and method name that pulled the exception ...

Example:

        Try
            ...some code
        Catch ex As Exception
            Msg.Text = "This error occured in this class: " & currentClassName & " and this method: " & currentMethodName & ex.Message
        End Try

Anyone got any ideas of how to make it happen in an automated fashion? ie. I don't want to MANUALLY go through each function and assign a variable to remember the name of the class and function.

Cheers,
Mark
LVL 2
msiedleAsked:
Who is Participating?
 
ctm5Commented:
This is pretty easy to do. The Exception can tell you the name of the method:

ex.TargetSite.GetCurrentMethod.Name

To get the name of the class, the only way I have found is to look at

ex.StackTrace

and parse it to get what you want.

ctm5
0
 
msiedleAuthor Commented:
Beautiful! Thanks very much :-)

M
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.