Results 1 to 6 of 6

Thread: Tips on Error Handling

  1. #1

    Thread Starter
    Lively Member vasanth2979's Avatar
    Join Date
    Sep 2006
    Location
    Port Louis, Mauritius
    Posts
    88

    Tips on Error Handling

    Is there a way of preventing system crash due to any error?

    I mean instead of it crashing can we prevent it by displaying something....

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Tips on Error Handling

    You can Use On Error Statament at the begining of the Any Event
    eg
    VB Code:
    1. Private sub Comand_click
    2. On Error Goto Err1
    3. 'You code
    4.  
    5.  
    6. Exit sub
    7.  Err1:
    8.        Msgbox err.description
    9.        exit sub
    10. End sub

  3. #3
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Tips on Error Handling

    Or you can use this also
    VB Code:
    1. Private sub Comand_click()
    2. On Error Goto Err1
    3. 'You code
    4.  
    5.  
    6. Exit sub
    7.  Err1:
    8.        Msgbox err.description
    9.        exit sub
    10.        'resume 0  'Try the same process
    11.        'resume next 'executes next process
    12. End sub

  4. #4
    Fanatic Member
    Join Date
    Jul 2006
    Location
    nasik,india
    Posts
    909

    Re: Tips on Error Handling

    or if you want to resume it
    VB Code:
    1. on error resume next
    WHETHER YOU SUCCEED OR FAIL IS NOT AS IMPORTANT AS WHETHER YOU TRIED YOUR BEST

  5. #5
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: Tips on Error Handling

    Use On Error

    A Visual Basic program uses the On Error statement to register error handling code. This statement can take one of three forms:

    * On Error GoTo 0
    * On Error Resume Next
    * On Error GoTo line

    These forms tell Visual Basic what it should do when the program encounters an error. The three forms are described in the following sections.

    On Error GoTo 0

    On Error GoTo 0 is relatively straightforward. It simply cancels any currently installed error handler assigned by a previous On Error GoTo line or On Error Resume Next. If the program encounters an error after this statement executes, it crashes.

    On Error Resume Next

    On Error Resume Next makes the program ignore errors. When it encounters an error, the program continues execution after the statement that caused the error. When a program uses On Error Resume Next, it should check the Err object after every operation that might cause an error. If the value Err.Number is nonzero, the operation caused an error and the program can take special action. The program should check Err.Number immediately after the statement in question. Certain other actions reset the Err object and remove the previous error information.

    Many programs use On Error Resume Next when they present a common dialog to the user. The CommonDialog control's CancelError property indicates whether the control should raise an error if the user cancels the dialog. The following code fragment shows how a program can use CancelError to decide whether to continue an action such as loading a file.
    VB Code:
    1. ' Generate an error if the user cancels.
    2. dlgOpenFile.CancelError = True
    3.  
    4. ' Ignore errors for now.
    5. On Error Resume Next
    6.  
    7. ' Present the dialog.
    8. dlgOpenFile.ShowOpen
    9.  
    10. ' See if there was an error.
    11. If Err.Number = cdlCancel Then
    12.     ' The user canceled. Do nothing.
    13.     Exit Sub
    14. ElseIf Err.Number <> 0 Then
    15.     ' Unknown error. Take more action.
    16.        
    17. End If
    18.  
    19. ' Resume normal error handling.
    20. On Error GoTo 0
    On Error GoTo Line

    The On Error GoTo line statement registers a new error handler. If the program encounters an error, it passes control to the error handler beginning at the indicated line number or label. The error handler can then take appropriate action.

    The following code shows a simple error handler that catches unexpected errors and describes them to the user.
    VB Code:
    1. Private Sub DoSomething()
    2.     ' Install the error handler.
    3.     On Error GoTo UnexpectedError
    4.  
    5.     ' Do stuff.
    6.  
    7.     ' Do not pass through into the error handler code.
    8.     Exit Sub
    9.  
    10. UnexpectedError:
    11.     ' Describe the error to the user.
    12.     MsgBox "Unexpected error" & _
    13.         Str$(Err.Number) & _
    14.         " in subroutine DoSomething." & _
    15.         vbCrLf & _
    16.         Err.Description
    17.     Exit Sub
    18. End Sub
    CS

  6. #6
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Tips on Error Handling

    If you have an existing program you can use ******* or MZ-Tools to add the error handling to each sub for you.

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