Why are errors in classes only trapped at runtime?

别说谁变了你拦得住时间么 提交于 2019-12-11 15:27:45

问题


I have a VB6 class with a method which raises an error:

Public Sub DoSomething
  ...
  err.Raise 12345, description:="Error message"
  ...
End Sub

This method is called from a form:

Public Sub ErrTest()
  On Error Goto err1
  obj.DoSomething
  Exit Sub
err1:
  MsgBox err.Description
End Sub

This works fine at runtime, but at design time the error handling does not work. Instead the VB6 IDE displays its standard message box from where I can go into debug mode or end the program.

Why does this happen? Can I prevent it?


回答1:


In the VB IDE, go to Tools, Option, General tab, Error trapping frame. I'm guessing you have it set to 'Break on All Errors', whereas you probably want 'Break on Unhandled Errors'.

Your Err.Raise statement gives a compile error for me; try removing the braces.

Also, you may want to use

Err.Raise vbObjectError + 12345, Description:="Error message"

i.e. offset your error code from the VB constant vbObjectError to be sure you don't get clashes.




回答2:


You can also change the error trapping options by right-clicking in the code window. The following options are available from the "Toggle" sub-menu:

Break on All Errors
Break in Class Module
Break on Unhandled Errors

I find this much easier than popping up the Options dialog...



来源:https://stackoverflow.com/questions/336665/why-are-errors-in-classes-only-trapped-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!