如何重新启用VB6中的默认错误处理 [英] How to re-enable the default error handling in VB6

查看:160
本文介绍了如何重新启用VB6中的默认错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个代码与各种On Error Goto错误处理程序在几个地方来处理一些破碎的第三方硬件。我在一个没有错误陷阱的例程中得到溢出错误(从Err变量读取),但是由例程调用。我一直认为错误陷阱只在他们声明的例程中有效,但是它看起来像一个子例程中的错误可能会导致它去调用函数的错误陷阱。

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.

所以我关闭了调用函数的错误陷阱,发现我的溢出,一切都很好。但在我这样做之前,我花了一些时间尝试找到一个程序性的方法来让VB返回到该例程中的默认错误处理(所以我不必修改外部代码进行调试),但我不能。唯一的错误命令我可以找到:

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

全部打开手动错误处理 - 有没有办法关闭它(返回到VB6默认值)? / p>

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

推荐答案

这是在VB6手册中详细介绍的错误处理层次结构 On Error Goto 0 禁用当前过程中的错误处理程序,而不是在调用它的过程中。

This is explained thoroughly in the VB6 manual under Error Handling Hierarchy. On Error Goto 0 disables the error handler in the current procedure, not in the procedures that called it.


如果过程中出现错误,
此过程没有启用的
错误处理程序,Visual通过调用列表中的待处理
过程来基本搜索
,然后执行第一个
启用的错误处理程序。如果
在调用列表中没有遇到启用错误
处理程序,那么
会出现一个默认的意外错误
消息并暂停执行。

If an error occurs in a procedure and this procedure doesn't have an enabled error handler, Visual Basic searches backward through the pending procedures in the calls list — and executes the first enabled error handler it finds. If it doesn't encounter an enabled error handler anywhere in the calls list, it presents a default unexpected error message and halts execution.

如其他人所说,您可以转到工具 - 选项一般选项卡,然后选择断开所有错误。这有效地禁用所有的On Error语句 - IDE会在每个错误上立即中断。

As others have said, you can go to Tools-Options-General tab and choose Break on all errors. That effectively disables all your On Error statements - the IDE will break immediately on every error.

如果您的VB6代码在正常操作中抛出错误,那么可能会令人烦恼。例如,当您检查文件是否存在,或者用户在通用对话框中按取消时。您不希望IDE在这些行上每次都会中断。但是,您可能会在所有事件处理程序中使用样板错误处理程序,以阻止程序崩溃出现意外错误。但是当您调试问题时,这是一个麻烦,因为IDE不会与错误中断。一个窍门是在IDE中运行时关闭这些错误处理程序,但将其保留在构建的可执行文件中。你这样做。

That can be irritating if your VB6 code throws errors as part of normal operation. For instance when you check whether a file exists, or when the user presses cancel in a common dialogue. You don't want the IDE to break every time on those lines. But you might have boilerplate error handlers in all your event handling procedures, to stop the program crashing out on unexpected errors. But they are a nuisance when you're debugging problems because the IDE doesn't break on the line with the error. One trick is to switch off those error handlers when running in the IDE, but keep them in the built executable. You do it like this.

将这些功能放入模块中。

Drop these functions into a module.

Public Function InIDE() As Boolean 
  Debug.Assert Not TestIDE(InIDE) 
End Function 

Private Function TestIDE(Test As Boolean) As Boolean 
  Test = True 
End Function 

然后你可以这样编写你的错误处理程序。

Then you can write your error handlers like this.

Private Sub Form_Load() 
  If Not InIDE() Then On Error Goto PreventCrashes 
  <lots of code> 
  Exit Sub 

PreventCrashes: 
  <report the error> 
End Sub 

这里。另一个提示 - 使用免费加载项 MZTools 自动添加这些样板错误处理程序。对于生产质量代码,您可以进一步,并在每个例程中添加一个错误处理程序来创建一个 ghetto堆栈跟踪。您可能还会在每个错误处理程序中立即记录错误。

Pinched from here. Another tip - use the free add-in MZTools to automatically add these boilerplate error handlers. For production-quality code, you could go further and put an error handler in every routine to create a ghetto stack trace. You might also log the errors immediately in every error handler.

编辑:Ant已正确指出 On Error Goto -1 VB.Net声明并且在VB6中无效。

Ant has correctly pointed out that On Error Goto -1 is a VB.Net statement and isn't valid in VB6.

编辑:Arvo和OneNerd有一些有趣的讨论来编写答案,最终在VB6错误处理中进行了拆卸。 此问题中的讨论也值得看看。

Arvo and OneNerd have written answers with some interesting discussion of emulating Finally teardown blocks in VB6 error handling. The discussion in this question is also worth a look.

这篇关于如何重新启用VB6中的默认错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆