code_junkie

How to handle errors in VB Script

Nov 11th, 2011
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. On Error Resume Next '' ignore errors
  2. SomeIgnorableFunction()
  3.  
  4. On Error GoTo 0 '' removes error ignoring
  5. SomeImportantFunction()
  6.  
  7. On Error GoTo HandleError '' on error will code jump to specified signal
  8. Dim a
  9. a = 15 / 0
  10.  
  11. GoTo Finish '' skips error handling
  12.  
  13. HandleError:
  14. Dim msg
  15. Set msg = Err.Description & vbCrLf & Err.Number
  16. MsgBox msg
  17.  
  18. Finish:
  19. '' there is end of sciprt
  20.  
  21. Option Explicit
  22.  
  23. On Error Resume Next ' Potential error coming up
  24. Dim MyArray(5)
  25. MyArray(7) = "BWA HA HA"
  26. If Err.Number <> 0 Then
  27. LogError(Err)
  28. Err.Clear
  29. End If
  30. On Error Goto 0 ' Stop looking for errors
  31.  
  32. Sub LogError(Details)
  33. Dim fs : Set fs = CreateObject("Scripting.FileSystemObject")
  34. Dim logFile : Set logFile = fs.OpenTextFile("c:errors.log", 8, True)
  35. logFile.WriteLine(Now() & ": Error: " & Details.Number & " Details: " & Details.Description)
  36. End Sub
  37.  
  38. private sub BucketList()
  39. do while 1=1
  40. ClimbMountain(top)
  41. if err.Number <> 0 then exit do
  42. SwimOcean(deep)
  43. if err.Number <> 0 then exit do
  44. GiveErrorHandlingToVBS(isNeverGoingToHappen)
  45. if err.Number <> 0 then exit do
  46.  
  47. exit do
  48. loop
  49.  
  50. 'Error Handler
  51. if err.Number <> 0 then
  52. 'handle error
  53. end if
  54.  
  55. end sub
Add Comment
Please, Sign In to add comment