1. 程式人生 > >VBS 模擬Try-Cathch異常處理的方法

VBS 模擬Try-Cathch異常處理的方法

Dim globalErrHanler   '定義全域性物件

Function FuncWithError()  '包含錯誤的方法
	Dim x,y
	x = 2 + 4
	y = x-2-4
	x = x/y
End Function

Function FuncWithErrHanler()     
	On Error Resume Next
	Call FuncWithError()  '異常處理
	If Err.Number <> 0 Then
		Call globalErrHanler()
	End If
	'WSH.Echo "aaa" 
End Function  


Public Function OnErrorGoTo(ByVal FunctionName)
	Set globalErrHanler = GetRef(FunctionName)
End Function


Public Function errHandler()
	WSH.Echo "Source = " & Err.Source & ", # = " & Err.Number &", Desc=" & Err.Description
End Function  

OnErrorGoTo "errHandler"

Call FuncWithErrHanler()