我有一个很好的例子,我需要帮助。我试图找到一种方法来处理我的错误,但保持我的子功能模块化(不需要知道我的其余代码是否正常工作)
在Excel VBA中,没有Try / Catch功能。 VBA中有本机函数,如果失败则返回错误。我期待以下示例整洁
Function wrapperForFunction(input As Variant) As Boolean
On Error goto returnTrue
fancyFunctionThatReturnsError(input As Variant)
wrapperForFunction = False
LINE OF CODE THAT SETS ERROR BACK TO WHAT IT WAS BEFORE CHANGING IT TO returnTrue
Return
returnTrue:
wrapperForFunction = True
LINE OF CODE THAT SETS ERROR BACK TO WHAT IT WAS BEFORE CHANGING IT TO returnTrue
End Function
Sub MainProgram()
On Error goto errorHandlerMain
'' do stuff
if wrapperForFunction(input) then 'do stuff
'' do morestuff
if wrapperForFunction(input) then 'do stuff
errorHandlerMain:
'non-erroring cleanup code
End Sub
Sub NestedSub()
On Error goto errorHandlerNestedSub
'' do stuff
if wrapperForFunction(input) then 'do stuff
errorHandlerNestedSub:
'non-erroring cleanup code
End Sub
我想避免的是这种解决方案
Function wrapperForFunction(input As Variant) As Boolean
On Error goto returnTrue
fancyFunctionThatReturnsError(input As Variant)
wrapperForFunction = False
Return
returnTrue:
wrapperForFunction = True
End Function
Sub MainProgram()
On Error goto errorHandlerMain
'' do stuff
tmp = wrapperForFunction(input) ' <------------ THIS LINE WAS ADDED
On Error goto errorHandlerMain ' <------------ THIS LINE WAS ADDED
if tmp then 'do stuff
'' do morestuff
tmp = wrapperForFunction(input) ' <------------ THIS LINE WAS ADDED
On Error goto errorHandlerMain ' <------------ THIS LINE WAS ADDED
if tmp then 'do stuff
errorHandlerMain:
'non-erroring cleanup code
End Sub
Sub NestedSub()
On Error goto errorHandlerNestedSub
'' do stuff
tmp = wrapperForFunction(input) ' <------------ THIS LINE WAS ADDED
On Error goto errorHandlerNestedSub ' <------------ THIS LINE WAS ADDED
if tmp then 'do stuff
errorHandlerNestedSub:
'non-erroring cleanup code
End Sub
有什么建议吗?有没有办法找出当前的&#34; On Error goto x&#34;状态是,并将其重置为更改之前的状态? 喜欢
Function foobar()
Dim errorLine As Long: errorLine = CURRENT_ERROR_LINE
On Error goto 40
'do stuff
On Error goto errorLine
Return
40'cleanup code
End Function
答案 0 :(得分:1)
关于VBA中的错误处理的几点注意事项:
On Error Goto <x>
语句的范围是包含语句的过程以及在该过程中调用的任何过程。考虑到上述行为,在每次调用过程后都没有必要重新声明On Error Goto <x>
,因为调用过程对其错误处理所做的任何更改都会在完成时超出范围
使用以下示例:
Public Sub Subroutine1()
Dim a As Double, b As Double, c As Double
On Error GoTo Err_Subroutine
a = 10
b = 2
c = Divide(a, b)
c = c / 0
Exit_Subroutine:
Exit Sub
Err_Subroutine:
Select Case Err.Number
Case 11 'DIV/0
MsgBox "DIV/0 error in Sub Subroutine1()"
Resume Exit_Subroutine
Case Else
MsgBox "Unhandled error in Subroutine1()"
Resume Exit_Subroutine
End Select
End Sub
'Returns a/b or 0 if an error occurs
Public Function Divide(a, b)
On Error GoTo Err_Divide
Divide = a / b
Exit_Divide:
Exit Function
Err_Divide:
Select Case Err.Number
Case 11 'DIV/0
MsgBox "DIV/0 error in Function Divide()"
Divide = 0
Resume Exit_Divide
Case Else
MsgBox "Unhandled error in Function Divide()"
Divide = 0
Resume Exit_Divide
End Select
End Function
尽管已调用Divide
并且语句On Error GoTo Err_Divide
已执行,但在下一行发生DIV / 0错误时,错误仍将定向到Err_Subroutine
。