What’s the best way to catch and report an error?

Rabid Lemming

New member
Joined
Feb 10, 2005
Messages
4
Programming Experience
Beginner
hey ya all,





I am a student learning or more to the point teaching my self vb.net. I am using the 2005 beta version free from Microsoft because I can not afford the fully licence version lol



Any way I am making an application and I want to be able to either trace or catch errors and report what the error is to the user



Normally I would use:



VB.NET:
 [/color]

[color=blue]On[/color][color=black] [/color][color=blue]Error[/color][color=black] [/color][color=blue]GoTo[/color][color=black] errorHandler[/color]

[color=black]errorhandler:[/color]

[color=black]txtHelp.Text = "Warining!!! A error has occurd!" + vbNewLine + "Please alert the administration and inform them of the following error:" + vbNewLine + Conversion.ErrorToString(EventLogEntryType.Error + EventLogEntryType.Information + EventLogEntryType.Warning)[/color]

[color=black]




But I think perhaps I should be using:



VB.NET:
[/color]

[color=blue]Private[/color][color=black] [/color][color=blue]Shared[/color][color=black] generalSwitch [/color][color=blue]As[/color][color=black] [/color][color=blue]New[/color][color=black] TraceSwitch("General", "Entire Application")[/color]

[color=blue]Public[/color][color=black] [/color][color=blue]Shared[/color][color=black] [/color][color=blue]Sub[/color][color=black] MyErrorMethod([/color][color=blue]ByVal[/color][color=black] myObject [/color][color=blue]As[/color][color=black] [/color][color=blue]Object[/color][color=black], [/color][color=blue]ByVal[/color][color=black] category [/color][color=blue]As[/color][color=black] [/color][color=blue]String[/color][color=black])[/color]

[color=green]' Write the message if the TraceSwitch level is set to Error or higher.[/color][color=black][/color]

[color=blue]If[/color][color=black] generalSwitch.TraceError [/color][color=blue]Then[/color][color=black][/color]

[color=black]txtHelp.Text = "Warining!!! A error has occurd!" + vbNewLine + "Please alert the administration and inform them of the following error:" + vbNewLine + myObject + category[/color]

[color=blue]End[/color][color=black] [/color][color=blue]If[/color][color=black][/color]

[color=green]' Write a second message if the TraceSwitch level is set to Verbose.[/color][color=black][/color]

[color=blue]If[/color][color=black] generalSwitch.TraceVerbose [/color][color=blue]Then[/color][color=black][/color]

[color=black]txtHelp.Text = "Warining!!! A error has occurd!" + vbNewLine + "Please alert the administration and inform them of the following error:" + vbNewLine + Conversion.ErrorToString(EventLogEntryType.Error + EventLogEntryType.Information + EventLogEntryType.Warning)[/color]

[color=blue]End[/color][color=black] [/color][color=blue]If[/color][color=black][/color]

[color=blue]End[/color][color=black] [/color][color=blue]Sub[/color][color=black] [/color][color=green]'MyErrorMethod [/color]

[color=black]




i am not 100% sure if either of these codes work or not as unless i get an error i cant test the code.



The question I am asking is what is the best way to catch any error and report the error back to the user?



Any help would be appreciated



Cheers

Rabid Lemming
 
This seems to work for me very well!

Try
' your program code

Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "")

End Try

 
Back
Top