Script stops on protected files such as system files

前端 未结 4 1942
轮回少年
轮回少年 2021-01-26 11:19

This code stops after a while due to protected files such as system files, \"Permission Denied\".

Is there a way to modify the code below so that it can handle such prot

4条回答
  •  时光取名叫无心
    2021-01-26 12:11

    VBScript allows error trapping, though not as gracefully as VBA. Try the script below.

    On Error Resume Next
        '[ ... code ...  ]
    Dim test_result, divisor
    
     divisor = 1        '' No error
    'divisor = 0        '' raise error #11
    'divisor = "zero"   '' raise a different error
    
    test_result = 2/divisor
    
    If Err.Number = 11 then  ''This line must appear at the point error is raised
        MsgBox "Handled Error: " & Err.Description
    ElseIf Err.Number > 0 then 
        MsgBox "Error: " & Err.Number & "  " & Err.Description
        Err.Clear   ''if you wanted to proceed clean from here
    End If
    
    MsgBox "Result: " & test_result
    

提交回复
热议问题