PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] Catching Errors with a Errorhandler-VBForums
Results 1 to 7 of 7

Thread: [RESOLVED] Catching Errors with a Errorhandler

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2009
    Posts
    27

    Resolved [RESOLVED] Catching Errors with a Errorhandler

    Hi,

    Does someone have a example to catch errors and log them to the Eventlog?

    I would like to catch any error and pass them to my WriteToEventLog function.

    If i use On Error , how could I catch the Error and pass it to my WriteToEventLog function?
    I would like to catch the actual error



    Public Function WriteToEventLog(ByVal entry As String, Optional ByVal appName As String = "CompanyName", Optional ByVal eventType As EventLogEntryType = EventLogEntryType.Information, Optional ByVal logName As String = "ProductName") As Boolean
    '*************************************************************

    'NAME: WriteToEventLog

    'PURPOSE: Write to Event Log

    '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

    ' LogNam1e: 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

    '*************************************************************
    Dim objEventLog As New EventLog

    Try

    'Register the Application as an Event Source

    If Not EventLog.SourceExists(appName) Then
    EventLog.CreateEventSource(appName, logName)
    End If

    'log the entry

    objEventLog.Source = appName
    objEventLog.WriteEntry(entry, eventType)

    Return True

    Catch Ex As Exception

    Return False

    End Try

    End Function

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,682

    Re: Catching Errors with a Errorhandler

    you are catching the error... that's what the "Catch ex As Exception" is.... that's your catch. Before you return, that's where you should be logging the error at.

    OH, I see what you posted now... in that case, you have an example of how to catch errors.... it's the "Try...Catch...Finally" syntax.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2009
    Posts
    27

    Re: Catching Errors with a Errorhandler

    Quote Originally Posted by techgnome View Post
    you are catching the error... that's what the "Catch ex As Exception" is.... that's your catch. Before you return, that's where you should be logging the error at.

    OH, I see what you posted now... in that case, you have an example of how to catch errors.... it's the "Try...Catch...Finally" syntax.

    -tg
    Yes you are right, but i want to pass the exeption to my WritetoEvenlog function..

    and

    i got this error when trying to convert it to string.
    "Value of type 'System.Exception' cannot be converted to 'String"

    Maybe my Iam on the wrong track..

    My goal is to pass it to the eventlog.

    Thanks

    /Stefan

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Nov 2009
    Posts
    27

    Re: Catching Errors with a Errorhandler

    Like this

    Dim ex as Exeption

    On error goto Errorhandler


    Sub Errorhandler (Error as exeption)

    msgbox"Error blabla"

    WriteToEventLog(Ex, "Myapp", EventLogEntryType.Error, "Application")

    Exit Sub

  5. #5
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,958

    Re: Catching Errors with a Errorhandler

    Just an example....

    VB.NET Code:
    1. Dim myEvLog As New EventLog("MineLog", ".", "MySource")
    2.         Try
    3.             Dim i As Integer = Integer.MaxValue - 1
    4.             'Something that raises an error
    5.             i += 10
    6.         Catch ex As Exception 'Error occurred
    7.             myEvLog.WriteEntry(ex.Message, EventLogEntryType.Error)
    8.  
    9.         End Try

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,682

    Re: Catching Errors with a Errorhandler

    no.....


    Code:
    Try
      'Do some code in here that could go wrong
    Catch ex As Exception
      WriteToEventLog(Ex.Message, "Myapp", EventLogEntryType.Error, "Application")
    End Try
    It's exactly the same as the example that is in your logger that you posted.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Nov 2009
    Posts
    27

    Re: Catching Errors with a Errorhandler

    Thanks!!!

    So easy..

    I Love this forum and all helpfull people!

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width