Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Return ON ERROR behaviour to default 2

Status
Not open for further replies.

msc0tt

IS-IT--Management
Jun 25, 2002
281
CA
Hello,
I have one location in my code where I want custom error trapping. After that, I wish the (non-)error handling of VB to return to normal. In other words, can I reverse the effects of my one and only ON ERROR GOTO statement?
-thanks

note: I know about disabling error trapping (ON ERROR GOTO 0), and don't want to do this.
 
Ok, Let's see if I can't cover all basises here.

If you are using error handling where you are doing

On Error Goto Error_Handler

later down the code you want to do custom error handling you can create another section and make the same call as above only after goto adressing the custom handler. Afterwards you can recall your custom error handler from the begining.
as such.

On Error GoTo Error_Handler
'Do steps you want
On Error GoTo Custom_Handler
'Do steps in custom error handling
On Error GoTo Error_Handler
'continues back on the original error handling.


Now if you are not doing error handling before the code you want in a custom error handling, then you need to do the following

'Do unhandled steps
On Error GoTo Custom_Handler
'Do Steps in custom error handling
On Error GoTo 0
'continue unhandled steps

blweb
 
Yep that's pretty much it ;-)

The only one you left out is Resmue Next

Sub OpenFile()
On Error Resume Next
Dialog.ShowOpen
If Err.Number <> 0 Then Exit Sub
'...Open The File, Etc...
End Sub

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Let me try again:

Open "no such file" For Input as #1 ' I get a std runtime error
On Error Goto 0
Open "no such file" For Input as #1 ' I get no error
On Error Goto (the way you were before)
Open "no such file" For Input as #1 ' I get a std runtime error again

It is the (way you were before) functionality I'm looking for.
-thanks
 
You may be confused. On Error Goto 0 does not disable all error trapping - it's the same thing as not setting up an error handler at all. It does disable an error trap you have set up with On Error Goto xxx. In this case, the VB runtime will do the trapping and force the program to end. Is this the behavior you want in your program?

"I think we're all Bozos on this bus!" - Firesign Theatre [jester]
 
Artie,
You are correct - I have misunderstood a reference: Quoting Peter Norton's "Guide to Visual Basic 6", page 252, "...the On Error GoTo 0 statement disables further error traps.".
I misinterpreted this statement. What is *really* means is On Error GoTo 0 returns error trapping to the default VBA error mechanism -which is what I want.
-thanks for the replies.
 
On Error Goto 0 does disable error handling...

If your program is compiled to EXE, your program will die... (and usually say what error it found)

If you want to just ignore all errors you use On Error Resume Next

You can then customize the way it handles errors by using the Err class... be sure to use Err.Clear when you are done with Error Handling...

You can set this up to work similar to the .Net/Java : Try/Catch methods...

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Hi Cube,

It's all about symantics... :-{) Here's the way I would put it:

On Error GoTo 0 ' errors handled by default VB mechanism
On Error GoTo myHandler ' errors handled by custom routine
On Error Resume Next ' disable error handling

-Mike
 
msc0tt,
Not really...

Error handling is when your program handles the errors internally...

On Error GoTo 0 ' disable error handling (Program Crashes)
On Error GoTo myHandler ' errors handled by custom routine
On Error Resume Next ' errors ignored and/or handled by If/Then or Select/Case statements

Try this....

Create a new Standard EXE project...
Add 3 buttons to the form...
Then place this code in the form's code module...

Code:
Option Explicit

Private Sub Form_Load()
  Command1.Caption = "Goto 0"
  Command2.Caption = "Goto Handler"
  Command3.Caption = "Resume Next"
End Sub

'****** Goto 0 example ********
Private Sub Command1_Click()
  On Error GoTo 0
  Dim X As Integer, Y As Integer
  Y = 0
  X = 1 / Y
  MsgBox "X = " & X
End Sub

'****** Goto Handler example ********
Private Sub Command2_Click()
  On Error GoTo HandleErr
  Dim X As Integer, Y As Integer
  Y = 0
  X = 1 / Y
  MsgBox "X = " & X
Exit Sub
HandleErr:
  Y = 1
  MsgBox "You Can't Divide by Zero." & vbCrLf & "Y replaced with 1"
  Resume
End Sub

'****** Resume Next example ********
Private Sub Command3_Click()
  On Error Resume Next
  Dim X As Integer, Y As Integer
  Y = 0
  X = 1 / Y
  If Err.Number Then
    Y = 1
    X = 1 / Y
    MsgBox "You Can't Divide by Zero." & vbCrLf & "Y replaced with 1"
    Err.Clear
  End If
  MsgBox "X = " & X
End Sub

If you compile and run the exe, the first button will crash the program, and the other 2 will handle the error...

Hope this helps ;-)
-Josh

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
BTW...

If you are wanting to know what the error was, you can use Err.Description like this...

Code:
Private Sub Command2_Click()
  On Error GoTo HandleErr
  Dim X As Integer, Y As Integer
  Y = 0
  X = 1 / Y
  MsgBox "X = " & X
Exit Sub
HandleErr:
  [b]MsgBox Err.Description[/b]
  Y = 1
  MsgBox "Y replaced with 1"
  Resume
End Sub
Or...
Code:
Private Sub Command3_Click()
  On Error Resume Next
  Dim X As Integer, Y As Integer
  Y = 0
  X = 1 / Y
  If Err.Number Then
    [b]MsgBox Err.Description[/b]
    Y = 1
    MsgBox "Y replaced with 1"
    X = 1 / Y
    Err.Clear
  End If
  MsgBox "X = " & X
End Sub

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Hey Josh,
Well, yes really....

I understand exactly what the different "On Error" constructs do. When an error occurs after an "On Error GoTo 0", the program terminates with a (sometimes) meaningful error. I consider this 'handling' the error, even though the program doesn't continue. When an error occurs after an "On Error Resume Next", the program continues as if nothing happened. I consider this 'not handling' the error (yes, additional code could check for error codes, but that's not my point).

Make no mistake, I'm not saying your understanding is wrong. I believe we both understand exactly what On Error does. Where we differ is in our own connotative definitions of what 'handling' means. Like I said (but misspelled), semantics. -cheers
 
blweb & CubeE101,

I just posted the same question...oops. I was pointed to this thread and wanted to thank you both for the help. *'s for each of you!

John

Every generalization is false, including this one.
-Unknown
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top