Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

error messages and nested functions

Status
Not open for further replies.
Jul 29, 2005
136
PT
Hi,

In my program i want to get an error message that shows in what function the error occurred. I have situations of having one function calling another functions (nested functions).

What is the best way to achieve my goals using vb.net? A simple throw will be overwritten by high level functions, right?

Thank you
 
Did you use try/catch blocks?

Try
' some code that may raise exception
Catch ex As {Exception_Type}
' handle it (the specified exception type)
.
.
Catch ex As Exception
' catch all exception types
Finally
' code to be executed whether or not there is exception
End Try

Chack also the 'ex.{methods}' like ex.Message.
 
I wrote a ErrorHandler class that passes errors into it and it displays the error message and logs it into one of my databases. In order to record the location of the error I have to hard-code the procedure/function name while calling the sub procedure. I can send you some sample code if you like.

Also, does anyone know how I can get the name of the procedure/event I am in programmatically. Sometimes, while copy-and-pasting my code, I forget to change the error location argument. It would be nice if I could use some type of function to get the location I am at.

-lucyv
 
Why hardcoding? I mentioned to have a look at ex.{methods}. Take a look at 'ex.TargetSite.Name' and 'ex.StackTrace' and the actual message: 'ex.Message'

?
 
If you just want the method name where an exception ocurred, it's available in the Exception object that gets passed to you in the Catch block:
[tt]Exception.TargetSite.Name[/tt]

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Hey chip, posted at the same time!

Also i would like to inform that visual studio 2005 (i dont know in 2003) that they is a handler to deal with unhandled exceptions. It is located at the Partial Class or My namespace.

Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException

End Sub

...
 
Thanks TipGiver & Chiph. I wasn't aware of Exception.TargetSite.Name
 
Using the Exception object, is there any find to find the code module where the error occured (i.e. basMain, frmLogin, etc)?

If so then you could find the exact location of the error similiar to: [bold]frmLogin_cmdSubmit_Click[/bold].

Thanks in advance for any info.

JodyJay
 
Sorry for the "[bold] [/bold]" typo. My "Preview Post" button is working. :)
 
??? I wrote 3 things. One of them was ".StackTrace"

Did you try this one?
 
Yes I did. But it gives me the entire name and path for my application, with the code module and line number at the end. I was wondering if there was some sort of scaled down function for this. If not, then I will have to parse the StrackTrace to find the info that I need/want.
 
jodyjay -
The TargetSite property of the Exception object is of type MethodBase, which has a property called ReflectedType, which is of type Type. And Type has a property called Name which is the name of the enclosing class.

So, if you follow recommended development practice of only one type per source file, and naming your source file the same as the class, you'll have your information.

Otherwise, if all you have is the class name, you'll have to do a search/grep for it (file not named the same as the class, or multiple classes/types per source file).

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Chip,

TargetSite.ReflectedType.Name is exactly what I've been looking for! Thank you for this.

JodyJay
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top