How to re-enable the default error handling in VB6

2019-04-23 13:03发布

I have some code with various "On Error Goto" error handlers in a few places to handle some broken third party hardware. I was getting an overflow error (read from the Err variable) in a routine that doesn't have an error trap but is called by a routine that does. I always thought error traps were only valid in the routine they were declared, but it looks like an error in a subroutine can cause it to go to the calling function's error trap.

So I turned off the calling function's error trap and found my overflow and all is well. But before I did that, I spent some time trying to find a programatic way to get VB to return to its default error handling inside that routine (so I wouldn't have to modify outside code to debug), but I couldn't. The only error commands I could find:

  On Error GoTo [label]
  On Error Resume Next
  On Error Goto 0
  On Error GoTo -1

all turn on the manual error handling - is there a way to turn it off (back to the VB6 default)?

8条回答
2楼-- · 2019-04-23 13:49

Here is what I do:

First turn on error handling like this if necessary in your Sub Main() or Sub Form_Load() Sub:

'-- turn on error handling
'
On Error GoTo 0
'
'-------------------------

Now errors will be turned on.

Next, Use the On Error Resume Next and On Error GoTo {label} commands in combination with the Err object. Here is an example of emulating a try/catch/finally:

Function MyFunction() as String

'-- start of error block
'
 On Error Goto Catch
   ' do something here that might cause an error
   MyFunction = "IT WORKED"
   Goto Finally

   Catch:
   ' error occured - do something else
   MyFunction = Err.Description
   Err.Clear

 Finally:
   ' put your finally code here

 '
 '-- end of error block

End Function
查看更多
迷人小祖宗
3楼-- · 2019-04-23 13:49

/Tools/Options/General/Error Handling

查看更多
登录 后发表回答