Does code know the name of the Sub of Function it is in?

  • Thread starter Thread starter Just_a_fan
  • Start date Start date
J

Just_a_fan

I wanted to write up a generic Try/Catch that I could put in everywhere
to find a very stubborn problem with "Publish"ed, distributed code (it
runs on about 50% of target XP machines). Something is missing from the
distribution and the program won't tell what.

So I wanted to be able to copy and paste in some code with a msgbox
telling what routine the code was bombing in. This would be the first
step to getting closer to what the problem really is.

Maybe that info is compiled out of distributed code and would not be
available.

Any ideas?

Mike
 
So I wanted to be able to copy and paste in some code with a msgbox
telling what routine the code was bombing in. This would be the first
step to getting closer to what the problem really is.

'MethodBase.GetCurrentMethod'.
 
Actually MethodBase.GetCurrentMethod.ToString
and add: Imports System.Reflection

Thanks. I would have probably NEVER found that little bit of
obfuscation!

Now, I can put that one line of code in each routine before the form
shows and start narrowing down where the missing piece of the Publish
distribution is. What fun...

If VB would only tell me what it could not find, it would save a LOT of
time I have already wasted and a bunch more I will be wasting!

Mike
 
Or you have the whole call stack in the excepttion object. So if you use
exception.tostrong in your global exception handler you should find the
whole call stack (including the current method).
 
Or you have the whole call stack in the excepttion object. So if you use
exception.tostrong in your global exception handler you should find the
whole call stack (including the current method).

To add to what Patrice has said, this is what I usually use when
reporting an exception:

Try
'something
Catch ex As Exception
Dim sb As New StringBuilder(256)
While ex IsNot Nothing
sb.AppendFormat("{0}: {1}{2}", ex.Message, ex.StackTrace,
vbCrLf)
ex = ex.InnnerException
End While
'Write sb.ToString() to a log file or display or whatever here.
End Try

That way I get all the details of the exception, including the method
names.

Chris
 
Chris said:
. . . this is what I usually use when reporting an exception:
Catch ex As Exception
Dim sb As New StringBuilder(256)
While ex IsNot Nothing
sb.AppendFormat("{0}: {1}{2}", ex.Message, ex.StackTrace,
vbCrLf)
ex = ex.InnerException
End While
'Write sb.ToString()

May I ask /why/?

I've always found

'Write( ex.ToString() )

to be perfectly adequate, returning (by default, anyway) the complete
text of the Exception /including/ all of its nested, inner Exceptions.

Regards,
Phill W.
 
May I ask /why/?

I've always found

'Write( ex.ToString() )

to be perfectly adequate, returning (by default, anyway) the complete
text of the Exception /including/ all of its nested, inner Exceptions.

I stand corrected. Looking at the ToString method in Reflector, I see
that it does traverse the call stack, recursively calling ToString on
each of the inner exceptions as well.

It's funny, I started recording exceptions like this awhile back and I
could have sworn that the inner exceptions were not being returned.
Oh well, live and learn.

Chris
 
Back
Top