Previous Section Table of Contents Next Section

Planning for Errors

Errors occur. There are actually a few different types of errors, with specific ways of dealing with each.

  • Syntax errors. These are simple typos that you introduce when writing a script. You'll generally catch these when you test your scripts.

  • Logic errors. These design flaws make the script behave unexpectedly or incorrectly. Again, these are usually your fault, and you'll find them as you test the script.

  • Conditional errors. These errors occur because something in the script's operating environment was other than what you planned for when you wrote the script. For example, a domain controller might be unavailable, or a user may have typed a server name that doesn't exist.

Syntax and logic errors often crop up in scripting, and you'll find them as you test and debug your scripts. Conditional errors, however, are generally beyond your control. Your scripts should try to anticipate these errors, however, and handle them gracefully. For example, suppose you're using the WScript.Network object to map a network drive, and the server happens to be unavailable at the time. The basic script might look like this:


Set oNet = WScript.CreateObject("Wscript.Network")

oNet.MapNetworkDrive "S:", "\\Server2\SalesDocs"

If Server2 isn't available, the script will crash when executing the second line of code. That means the script will display an error message and won't execute anything else in the script. You can make the script a bit more resilient by anticipating the problem and adding error-handling code.


Set oNet = WScript.CreateObject("Wscript.Network")



On Error Resume Next

oNet.MapNetworkDrive "S:", "\\Server2\SalesDocs"

If Err <> 0 Then

 MsgBox "Server2 was unavailable; your S: drive was not mapped."

End If

On Error Goto 0

This modified script starts out by telling VBScript, "Look, if an error occurs, it's OK, I'll handle it. You just resume execution with the next line of script." That's done by On Error Resume Next.

After trying to map the drive, an If…Then construct checks the value of the special variable Err. If it's zero, the drive was mapped. If not, a friendlier error message is displayed to the user letting him know something went wrong. Finally, error checking is turned off with On Error Goto 0. From then on, errors will result in a VBScript error message and the script will stop executing.

If…Then is introduced under "Conditional Execution" in Chapter 10. Variables are covered under "What Are Variables?" in Chapter 5. I'll cover error handling in more detail throughout the book.

NOTE

As you can see, error trapping and handling can add bulk to a script. To help keep the examples in this script focused on the task, I'll usually omit error handling. However, all scripts meant to run in the real world should include error handling wherever something might go wrong.


Error handling needs to be something you plan for in your initial script design. Listing 4.6 shows how you might make a note of possible conditional errors in your original task list.

Listing 4.6. Identifying possible errors. Anticipating errors in your design will show you where to add error-handling code to your script.

Login script task list v3 with error notes



Display a logon welcome message:

"Welcome to BrainCore.Net. You are now logged on."

Wait until the user presses OK to dismiss the welcome message.

* Can't think of any potential errors here.



Map the N: drive:

Obtain a list of Domain Users group members. See if the user is in the 

  list. If they are, map the drive to \\Server\Users.

* Server might be unavailable, need to handle this.



Map the R: drive:

Obtain a list of Research group members. See if the user is in the 

  list. If they are, map the drive to \\Server2\ResearchDocs.

* Server might be unavailable, need to handle this.



Map the S: drive:

Obtain a list of Sales group members. See if the user is in the list. 

  If they are, map the drive to \\Server2\SalesDocs.

* Server might be unavailable, need to handle this.



Map a printer:

Get the user's IP address. Look at just the third octet. To find it, 

  look for the last period in the IP address, and then the next-to-the-

  last period. The third octet is between the two periods. If the octet 

  is 100, map to \\NYDC\HPColor3. If it's 110, map to \\LADC\HP6. If

  it's 120, map to \\TXDC1\LaserJet. Then make the mapped printer the 

  default.

* Printer or server might be unavailable. Need to handle this if 

  it occurs.

Anticipating errors and handling them within the script is definitely the mark of a careful, experienced scripter. Plan for errors in every script you write and you'll definitely be more appreciated by the folks who use your scripts.

    Previous Section Table of Contents Next Section