PowerPoint 演示文稿

例11.17 处理被零除、溢出和非法的过程调用三种情况产生的错误。
Function Divide (numer, denom) as Variant
Const mnErrDivByZero = 11 ' 用于代表被零除
Const mnErrOverFlow = 6 ' 溢出
Const mnErrBadCall = 5 ' 非法的过程调用
On Error GoTo MathHandler
Dim Msg as String
Divide = numer / denom ' 计算
Exit Function
MathHandler:
If Err.Number = MnErrDivByZero Or Err.Number = ErrOverFlow _
Or Err = ErrBadCall Then
Divide = Null '产生错误则返回 NULL
Else
' 显示意想不到的错误信息。
Msg = "Unanticipated error " & Err.Number
Msg = Msg & ": " & Err.Description
MsgBox Msg, vbExclamation
End If
Resume Next ' 不管什么情况，Resume Next
End Function