Results 1 to 6 of 6

Thread: [RESOLVED] [2008] Help with creating an error handler

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    Resolved [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


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Help with creating an error handler

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Re: [2008] Help with creating an error handler

    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

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    Re: [2008] Help with creating an error handler

    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


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    Re: [2008] Help with creating an error handler

    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.
    Attached Files Attached Files
    "Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

  6. #6
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Re: [2008] Help with creating an error handler

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width