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....
Printable View
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....
You can Use On Error Statament at the begining of the Any Event
eg
VB Code:
Private sub Comand_click On Error Goto Err1 'You code Exit sub Err1: Msgbox err.description exit sub End sub
Or you can use this also
VB Code:
Private sub Comand_click() On Error Goto Err1 'You code Exit sub Err1: Msgbox err.description exit sub 'resume 0 'Try the same process 'resume next 'executes next process End sub
or if you want to resume it
VB Code:
on error resume next
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.On Error GoTo LineVB Code:
' Generate an error if the user cancels. dlgOpenFile.CancelError = True ' Ignore errors for now. On Error Resume Next ' Present the dialog. dlgOpenFile.ShowOpen ' See if there was an error. If Err.Number = cdlCancel Then ' The user canceled. Do nothing. Exit Sub ElseIf Err.Number <> 0 Then ' Unknown error. Take more action. End If ' Resume normal error handling. On Error GoTo 0
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:
Private Sub DoSomething() ' Install the error handler. On Error GoTo UnexpectedError ' Do stuff. ' Do not pass through into the error handler code. Exit Sub UnexpectedError: ' Describe the error to the user. MsgBox "Unexpected error" & _ Str$(Err.Number) & _ " in subroutine DoSomething." & _ vbCrLf & _ Err.Description Exit Sub End Sub
If you have an existing program you can use ******* or MZ-Tools to add the error handling to each sub for you.