不能嵌套。 Try1: On Error Goto Catch1 'code1 Exit Sub Catch1: Resume Try2 Try2: On Error Goto Catch2 'code2 Exit Sub Catch2: ...
个人不喜欢这种goto结构,相比更喜欢用 on error resume next 结构代替,然后里面加if判断可能出错的情况
on error resume next 不是说 碰到错误就继续下一语句吗? 用if判断的话 不是要写很多?
将code2写成一个子过程,比如 Sub code2() On Error Goto Catch3 '... exit sub Catch3: '... end sub '但这种方法在子过程code2中无法访问原过程或函数里面的局部变量
用on resume next, goto换个位置,用在判断之后,可很方便得到你想要的。 Private Sub Command1_Click() On Error Resume Next Dim errFlag As Boolean errFlag = True 'errFlag = False
If errFlag Then Err.Raise 9 '制造错误 Else Debug.Print "OK" End If
If Err.Number Then GoTo ierr1 Exit Sub ierr1: Debug.Print "ERR 1" Debug.Print Err.Description
Err.Clear
Err.Raise 11 If Err.Number Then GoTo ierr2 Exit Sub ierr2: Debug.Print "ERR 2" Debug.Print Err.Description Err.Clear
End Sub Private Sub Command1_Click() On Error Resume Next Dim errFlag As Boolean errFlag = True 'errFlag = False
If errFlag Then Err.Raise 9 '制造错误 Else Debug.Print "OK" End If
If Err.Number Then GoTo ierr1 Exit Sub ierr1: Debug.Print "ERR 1" Debug.Print Err.Description
Err.Clear
Err.Raise 11 If Err.Number Then GoTo ierr2 Exit Sub ierr2: Debug.Print "ERR 2" Debug.Print Err.Description Err.Clear
End Sub
上面方法的优点是捕获并处理错误后总能Resume Next,比较下面的代码: 点Command1输出1354,点Command2输出13542 Private Sub Command1_Click() On Error GoTo Catch1 Debug.Print "1" Debug.Print 1 / 0 Debug.Print "2" Exit Sub Catch1: Resume Try2 Try2: On Error GoTo Catch2 Debug.Print "3" Debug.Print 1 / 0 Debug.Print "4" Exit Sub Catch2: Debug.Print "5" Resume Next End SubPrivate Sub Command2_Click() On Error GoTo Catch1 Debug.Print "1" Debug.Print 1 / 0 Debug.Print "2" Exit Sub Catch1: CatchOne Resume Next End Sub Sub CatchOne() On Error GoTo Catch2 Debug.Print "3" Debug.Print 1 / 0 Debug.Print "4" Exit Sub Catch2: Debug.Print "5" Resume Next End Sub
用on resume next, goto换个位置,用在判断之后,可很方便得到你想要的。Private Sub Command1_Click() On Error Resume Next Dim errFlag As Boolean errFlag = True 'errFlag = False
If errFlag Then Err.Raise 9 '制造错误 Else Debug.Print "OK" End If
If Err.Number Then GoTo ierr1 Exit Sub ierr1: Debug.Print "ERR 1" Debug.Print Err.Description
Err.Clear
Err.Raise 11 If Err.Number Then GoTo ierr2 Exit Sub ierr2: Debug.Print "ERR 2" Debug.Print Err.Description Err.Clear
Try1:
On Error Goto Catch1
'code1
Exit Sub
Catch1:
Resume Try2
Try2:
On Error Goto Catch2
'code2
Exit Sub
Catch2:
...
on error resume next 结构代替,然后里面加if判断可能出错的情况
Sub code2()
On Error Goto Catch3
'...
exit sub
Catch3:
'...
end sub
'但这种方法在子过程code2中无法访问原过程或函数里面的局部变量
Private Sub Command1_Click()
On Error Resume Next
Dim errFlag As Boolean
errFlag = True
'errFlag = False
If errFlag Then
Err.Raise 9 '制造错误
Else
Debug.Print "OK"
End If
If Err.Number Then GoTo ierr1
Exit Sub
ierr1:
Debug.Print "ERR 1"
Debug.Print Err.Description
Err.Clear
Err.Raise 11
If Err.Number Then GoTo ierr2
Exit Sub
ierr2:
Debug.Print "ERR 2"
Debug.Print Err.Description
Err.Clear
End Sub
Private Sub Command1_Click()
On Error Resume Next
Dim errFlag As Boolean
errFlag = True
'errFlag = False
If errFlag Then
Err.Raise 9 '制造错误
Else
Debug.Print "OK"
End If
If Err.Number Then GoTo ierr1
Exit Sub
ierr1:
Debug.Print "ERR 1"
Debug.Print Err.Description
Err.Clear
Err.Raise 11
If Err.Number Then GoTo ierr2
Exit Sub
ierr2:
Debug.Print "ERR 2"
Debug.Print Err.Description
Err.Clear
End Sub
点Command1输出1354,点Command2输出13542
Private Sub Command1_Click()
On Error GoTo Catch1
Debug.Print "1"
Debug.Print 1 / 0
Debug.Print "2"
Exit Sub
Catch1:
Resume Try2
Try2:
On Error GoTo Catch2
Debug.Print "3"
Debug.Print 1 / 0
Debug.Print "4"
Exit Sub
Catch2:
Debug.Print "5"
Resume Next
End SubPrivate Sub Command2_Click()
On Error GoTo Catch1
Debug.Print "1"
Debug.Print 1 / 0
Debug.Print "2"
Exit Sub
Catch1:
CatchOne
Resume Next
End Sub
Sub CatchOne()
On Error GoTo Catch2
Debug.Print "3"
Debug.Print 1 / 0
Debug.Print "4"
Exit Sub
Catch2:
Debug.Print "5"
Resume Next
End Sub
On Error Resume Next
Dim errFlag As Boolean
errFlag = True
'errFlag = False
If errFlag Then
Err.Raise 9 '制造错误
Else
Debug.Print "OK"
End If
If Err.Number Then GoTo ierr1
Exit Sub
ierr1:
Debug.Print "ERR 1"
Debug.Print Err.Description
Err.Clear
Err.Raise 11
If Err.Number Then GoTo ierr2
Exit Sub
ierr2:
Debug.Print "ERR 2"
Debug.Print Err.Description
Err.Clear
End Sub