Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2> Handling Errors </h2> <p>A sort of an "older style" of error handling is available to us in VBScript, that does make use of <code>On Error Resume Next</code>. First we enable that (often at the top of a file; but you may use it <em>in place of</em> the first <code>Err.Clear</code> below for their combined effect), then before running our possibly-error-generating code, clear any errors that have already occurred, run the possibly-error-generating code, and then explicitly check for errors:</p> <pre><code>On Error Resume Next ' ... ' Other Code Here (that may have raised an Error) ' ... Err.Clear ' Clear any possible Error that previous code raised Set myObj = CreateObject("SomeKindOfClassThatDoesNotExist") If Err.Number &lt;&gt; 0 Then WScript.Echo "Error: " &amp; Err.Number WScript.Echo "Error (Hex): " &amp; Hex(Err.Number) WScript.Echo "Source: " &amp; Err.Source WScript.Echo "Description: " &amp; Err.Description Err.Clear ' Clear the Error End If On Error Goto 0 ' Don't resume on Error WScript.Echo "This text will always print." </code></pre> <p>Above, we're just printing out the error if it occurred. If the error was fatal to the script, you could replace the second <code>Err.clear</code> with <code>WScript.Quit(Err.Number)</code>.</p> <p>Also note the <code>On Error Goto 0</code> which turns off resuming execution at the next statement when an error occurs.</p> <p>If you want to test behavior for when the <code>Set</code> succeeds, go ahead and comment that line out, or create an object that will succeed, such as <code>vbscript.regexp</code>.</p> <p>The <code>On Error</code> directive only affects the current running scope (current <code>Sub</code> or <code>Function</code>) and does not affect calling or called scopes.</p> <hr> <h2> Raising Errors </h2> <p>If you want to check some sort of state and then raise an error to be handled by code that calls your function, you would use <code>Err.Raise</code>. <code>Err.Raise</code> takes up to five arguments, <code>Number</code>, <code>Source</code>, <code>Description</code>, <code>HelpFile</code>, and <code>HelpContext</code>. Using help files and contexts is beyond the scope of this text. <code>Number</code> is an error number you choose, <code>Source</code> is the name of your application/class/object/property that is raising the error, and <code>Description</code> is a short description of the error that occurred.</p> <pre><code>If MyValue &lt;&gt; 42 Then Err.Raise(42, "HitchhikerMatrix", "There is no spoon!") End If </code></pre> <p>You could then handle the raised error as discussed above.</p> <hr> <p><sub><strong>Change Log</strong></sub> <li><sub>Edit #1: Added an <code>Err.Clear</code> before the possibly error causing line to clear any previous errors that may have been ignored. </sub></li> <li><sub>Edit #2: Clarified.</sub></li> <li><sub>Edit #3: Added comments in code block. Clarified that there was expected to be more code between <code>On Error Resume Next</code> and <code>Err.Clear</code>. Fixed some grammar to be less awkward. Added info on <code>Err.Raise</code>. Formatting. </sub></li></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload