【发布时间】:2018-01-04 18:55:57
【问题描述】:
假设您在主子中定义了 On Error。
Sub Main()
On Error Goto CatchAll
'... Some code... goes here
Call XYZ
CatchAll:
Msgbox "An Unexpected Error Occurred"
End
End Sub
在 Main sub 中,您可以调用例程 XYZ。 假设 Sub XYZ 是这样的:
Sub XYZ()
'If unexpected error happens here, control will be shifted to sub Main, Label CatchAll
On Error Goto Errorz
'If unexpected error happens here, control will be shifted to same sub, Label Errorz...
Errorz:
Msgbox "You have crashed inside XYZ"
End
End Sub
注意在 sub XYZ 中输入的 cmets。 也就是说,在经历程序崩溃后控制转移的地方是基于最后的“On Error Goto”语句。
有没有办法在 VBA 中恢复旧的 On Error Goto?
换句话说,在 Sub XYZ 中,我有一些代码:
Sub XYZ()
On Error Goto Errorz:
'Some Code
On Error Goto <Old Error Trapping Method>
'Here I desire to go back to Main CatchAll: label. Is there a way to do that?
End Sub
请注意上面代码中的最后一条注释。我希望能够重新定义 On Error 以在定义新行为之前恢复 On error 的最后一个行为(最后一个行为:Goto Main.CatchAll 标签、新行为、Goto XYZ.Errorz 标签)。在代码的这一点上,我希望能够说,出错时转到 Main.CatchAll。
有没有办法做到这一点?
【问题讨论】: