上次这个帖子结贴了,只好发个新帖。看MSDN里的话:Private Sub Class_Terminate()
On Error Resume Next
Debug.Print "Terminate Thing " & DebugID _
& ", Name=" & Name
End SubImportant You should always handle errors in the Class_Terminate event procedure. Errors in Class_Terminate cannot be handled by applications that use your component, and will therefore be fatal to the application.请问为何说“Errors in Class_Terminate cannot be handled by applications that use your component”?
On Error Resume Next
Debug.Print "Terminate Thing " & DebugID _
& ", Name=" & Name
End SubImportant You should always handle errors in the Class_Terminate event procedure. Errors in Class_Terminate cannot be handled by applications that use your component, and will therefore be fatal to the application.请问为何说“Errors in Class_Terminate cannot be handled by applications that use your component”?
对这个sub的On Error Resume Next以下的语句(和子sub、function)起作用。
Option ExplicitPrivate Sub Form_Load()
On Error GoTo err
Dim cls As New Class1
cls.xx
Set cls = Nothing
Exit Sub
err:
Debug.Print 1
End Sub
Option ExplicitPublic Sub xx()
'On Error GoTo err
Debug.Print 1 / 0
Exit Sub
'err:
' Debug.Print 2
End Sub那段话大概可以用这样一个例子来解释,有没有注释执行效果是完全不同的。
比如有些部件产生的错误。
举个例子:你只在VB的图上 画上了个MSCOMM1 ,假如你程序中写上了MSCOMM2,那么如果当运行到这句ON ERROR RESUME NEXTmscomm2.comport = 1仍然会出现错误。是个fatal的错误-------------------- 致命!!
我觉得这段话就是我上面所讲的。请楼主 翻译一下这段话到底是什么意思???直译就好!!不要加上自己的想象。You should always handle errors in the Class_Terminate event procedure. Errors in Class_Terminate cannot be handled by applications that use your component, and will therefore be fatal to the application.
你应该始终在 Class_Terminate 事件过程里处理异常。Class_Terminate 里的异常无法被使用你的部件的应用程序处理,因此这种异常(如果不在Class_Terminate 事件过程里处理的话)对应用程序就会是致命的。举例
比如说,你开发了一个ActiveX控件,供Project1使用。你的控件里的类的Class_Terminate里的错误就不能被Project1处理,必须在类的Class_Terminate里处理。
================================
====================================请问我举个这个例子中出现的 ON ERROR RESUME NEXT 不能处理的错误 是怎么个情况???
请解释一下!!!!
我也是写一个程序的时候忽然发现在类内部触发错误,外面捕捉不到(3楼那种)。Initialize事件倒没注意。
3楼AisaC贴出来的那种,无论on error goto 还是 resume next,都处理不了类内部发生的错误。
你说的对,刚才试了。不能捕获的错误,即使Resume Next也还是会有致命错。另外,我还试了Class_Initialize事件,它里面的错误是可以被外部进程捕获的。
貌似不能的,我本来就是这么设置的。Class_Terminate事件里的错误外部不能捕获。如果你是说其他事件里的错误,那么它是可以捕获的(我试了Class_Initialize事件)。
以上只是猜测,总之lz很细心,的确不能在Class_Initialize向外抛出异常。
如果由于外部调用引起隐式释放对象,触发 Class_Terminate,它的错误不能被你捕获。
反正都是引用计数减一。vb只是由引用计数为0来销毁对象。即使是显式释放,一样不能捕获
terminate事件中的错误,因为此种情况下,仍然是vb运行环境来调用terminate而不是用户程序。
为什么访问对象时,要用on error resume next 而不能用 on error goto 呢?
此贴:
再问 On Error Resume Next
引自MSDN的英文选段:
Important You should always handle errors in the Class_Terminate event procedure. Errors in Class_Terminate cannot be handled by applications that use your component, and will therefore be fatal to the application. 1、原贴内容谈的是on error goto 与 on error resume next区别;
2、本贴题目给人第一感觉就是原贴的延伸;
3、英文的内容指的是类内部错误处理,英文最后还有一问,本贴目前讨论的主要就是针对英文内容的;由上面4点可以看出,本贴有两个问题:
1、on error goto 与 on error resume next;
2、类内部错误处理;那标题还合适吗?
嘿嘿。