[RESOLVED] [2008] Help with creating an error handler
Ok i would like to create an error handler that would pop up anytime there is an error and state the type of error and number if possible. An example i tested was
Code:
Try
'Any Protected Code
prcTextFile.StartInfo.FileName = ("C:\Program Files\ErrorHandler\VbNetError.txt ")
prcTextFile.Start ()
If blnFlg = True Then Exit Try
Catch
'Error Handling Logic/Code
MessageBox.Show ("Unable to locate the desired file")
Finally
'Execution Resumes Here
MessageBox.Show ("Error Handler Complete")
End Try
And it worked fine but i want to know if this can be used to handle any kind of error and what other options are there.
Edit* To be more clear this is what im hoping to find something for. Lets say your running a program and an error occurs. Instead of giving you the windows message box it catches the error and send it to your error handler along with the Error number and message of the error. Thats what im hoping for and i know its possible im just not 100% sure on how to do it.
I know that declarations have to be used and something like this to show up in the error handler:
"Textbox1(Error.Name & " (" & ErrorMessage.ToString ")")"
Last edited by youngbucks; Jul 4th, 2008 at 01:19 AM.
"Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz
I'm not sure you understand exception handling fully. You should be adding a Try...catch block anywhere and everywhere in your code that an exception could reasonably be thrown AND that you want to catch it. There are many reasons not to catch exceptions in a particular place, even if you know that one may be thrown. Often you'll specifically want it to be caught elsewhere. If you want your code to carry on after an exception is thrown then you must catch it locally.
You can add a "last line of defence" in VB apps by handling your app's UnhandledException event. This will catch any exceptions that have not been explicitly handled elsewhere. You cannot simply resume executing code from that point though. The only safe thing to do is to exit the app because you have no idea what went wrong so how can you trust any data the app contains?
In addition to all that advice above, if you are writing to say, an event log or text file and recording each error in a form of debug or monitoring log style for your program (which you can use to help troubleshoot issues from, not relying on users to write down the error number and message shown to them) and want to place the reuseable code of this log writing into a new method, this may be a valid request. However I'd read up fully on how errors are raised, handled, affect the flow of code execution, their expensive resource usage and the like before you attempt to start this. Once you've gained this knowledge though, you could then write yourself a method which accepts an event as an argument, and call this from each chosen method as required:
Code:
Shared Sub WriteToEventLog(ByVal exceptionEncountered as exception)
I'd go along with all the above provided advice, and would add a further point too that often you want to catch specific exceptions and handle them differently which the above line/code simply won't give you the flexibility of performing. For an example, see this post which interacts with several different types of exceptions accordingly.
Last edited by alex_read; Jul 4th, 2008 at 02:17 AM.
Please rate this post if it was useful for you!
Please try to search before creating a new post,
Please format code using [ code ][ /code ], and
Post sample code, error details & problem details
I found something that i can use for writing the eventlog but i want it to show up in the textbox. Is there a way for modifying this to suit that because when i try i keep getting an error saying i cant convert it to System.windows.forms.textbox
Code:
Imports System.Diagnostics
Code:
Public Function WriteToEventLog(ByVal Entry As String, _
Optional ByVal AppName As String = "VB.NET Application", _
Optional ByVal EventType As _
EventLogEntryType = EventLogEntryType.Information, _
Optional ByVal LogName As String = "Application") As Boolean
'*************************************************************
'PURPOSE: Write Entry to Event Log using VB.NET
'PARAMETERS: Entry - Value to Write
' AppName - Name of Client Application. Needed
' because before writing to event log, you must
' have a named EventLog source.
' EventType - Entry Type, from EventLogEntryType
' Structure e.g., EventLogEntryType.Warning,
' EventLogEntryType.Error
' LogName: Name of Log (System, Application;
' Security is read-only) If you
' specify a non-existent log, the log will be
' created
'RETURNS: True if successful, false if not
'EXAMPLES:
'1. Simple Example, Accepting All Defaults
' WriteToEventLog "Hello Event Log"
'2. Specify EventSource, EventType, and LogName
' WriteToEventLog("Danger, Danger, Danger", "MyVbApp", _
' EventLogEntryType.Warning, "System")
'
'NOTE: EventSources are tightly tied to their log.
' So don't use the same source name for different
' logs, and vice versa
'******************************************************
Dim objEventLog As New EventLog()
Try
'Register the App as an Event Source
If Not objEventLog.SourceExists(AppName) Then
objEventLog.CreateEventSource(AppName, LogName)
End If
objEventLog.Source = AppName
'WriteEntry is overloaded; this is one
'of 10 ways to call it
objEventLog.WriteEntry(Entry, EventType)
Return True
Catch Ex As Exception
Return False
End Try
End Function
"Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz
I found one even better and easier to use which i understand but if someone can please help me find where to edit the StackTrace and Message to show the actual error that would come up in the windows Msgbox it would be greatly appreciated. Ive uploaded the source code.
"Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz
If you wanted a generic exception handler for a medium to large scale project, then Microsoft provides an Exception Handling Application Block as part of the Enterprise Library which gives a lot more flexibiltiy than that sample posted.
If you are merely wanting to output the message to a messagebox, I would urge you to simply place messagebox.show() calls in each of your try...catch blocks of code.
Please rate this post if it was useful for you!
Please try to search before creating a new post,
Please format code using [ code ][ /code ], and
Post sample code, error details & problem details