Windows Scripting
  Home arrow Windows Scripting arrow Page 2 - Error Trapping and Capturing Third-Party O...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Error Trapping and Capturing Third-Party Output in WSH
By: Nilpo/Developer Shed Staff Writer
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2007-03-05

    Table of Contents:
  • Error Trapping and Capturing Third-Party Output in WSH
  • Trapping Errors
  • Logging to the Event Log
  • Capturing Third-Party Output

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Error Trapping and Capturing Third-Party Output in WSH - Trapping Errors


    (Page 2 of 4 )

    The Err object is used for every error that occurs.  Each time you read its value, you must clear it before moving on with the Clear method.  You may also choose to create your own errors in some circumstances.  You can do that with the Raise method.

    The Err object is initialized by the VBScript Engine every time a script is executed.  You do not need to instantiate it in your code to use it.

    A basic If routine is all you need for an error check.  You’re simply going to query the Err number property to see if it’s anything other than 0.  Try this for example:

    If Err.Number <> 0 Then

       Wscript.Echo "Error: " & Err.Number

       Wscript.Echo "Description: " & Err.Description

       Wscript.Echo "Source: " & Err.Source

       Err.Clear

    End If

    This is a simple error-check.  We can use this same code to write to a log file instead.  The key here to is remember to clear the error object to prevent it from combining multiple errors.

    At times it may be useful to flag errors while using your own error-handling method.  Let’s say your code is designed to respond to a certain error.  In that case we can still use the Err object for logging purposes.

    Set objFso = CreateObject("Scripting.FileSystemObject")

     

    If objFso.FileExists("nosuch.file") Then

       Err.Raise 53,, "Cannot find specified file"

       Call errChk

       objFso.CreateTextFile("nosuch.file")

    End If

     

    Sub errChk

       If Err.number <> 0 Then

           Wscript.Echo "Error: " & Err.Number

           Wscript.Echo "Description: " & Err.Description

           Wscript.Echo "Source: " & Err.Source

           Err.Clear

       End If

    End Sub

    Here we’ve manually raised an Error 53 which is a File Not Found error.  Our script is set to handle this by creating the file that doesn’t exist but our error handler still gets a chance to log it if we so choose.

    So why did we have to create the error?  The answer is simple; because the FileExists method returns an error code of 0.  The method was able to complete successfully.

    More Windows Scripting Articles
    More By Nilpo/Developer Shed Staff Writer


       · Hey, guys. In this article I'm wrapping up my logging concept by showing you how to...
     

    WINDOWS SCRIPTING ARTICLES

    - Introducing Two-Way Data Binding using Silve...
    - Silverlight 2.0 Application Development with...
    - Burning Multisession CDs with IMAPI2 in WSH
    - Creating a Silverlight 2.0 Application that ...
    - Burning CDs with the IMAPI2 Control
    - Burning CDs in Windows XP with WSH
    - Advanced Word Object Scripting
    - Reading and Printing Word Documents in WSH
    - Scripting Microsoft Word
    - Using WSH to Catalog MP3 Files
    - Reading MP3 ID3 Tags in WSH
    - A Brief Look at Menus in WPF
    - More Examples of Simplified Image Processing...
    - Completing a WPF To-Do List Application
    - Simplified Image Processing in GDI+





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT