Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Error Message

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,45108
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Error Message
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1] 2   next >   >>
Login
Message << Older Topic   Newer Topic >>
 Error Message - 3/27/2007 9:29:42 PM   
  morpheus83uk

 

Posts: 305
Score: 0
Joined: 8/21/2006
Status: offline
Hello,

I have the following code:


      

I run my script and it wrote the following into the maintenance_error_log.txt:

Script Error: 28/03/2007 11:17:48
Script Name: Maintenance._Quick.vbs
Error: 0
Error (Hex): 0
Source:
Description:

I take it that this just means that the scripts has completed without any errors?

If this is the case my error reporting is working as I wish I just want it to report actual Errors opposed to if the script completes successfully. The reason being is I have a part of the script where by once the error log is populated it will send an E-Mail off reporting the error... now if each time this is being run it reports it as an error then we will get lots of E-Mails saying its completed successfully which would be a pain by any standards...

How could I achive me little goal?

Many Thanks

James

 
 
Post #: 1
 
 RE: Error Message - 3/28/2007 1:06:21 AM   
  ehvbs

 

Posts: 2106
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi morpheus83uk,

iErr = Err.Number
This will store the current value of Err.Number in iErr. iErr will either be 0 (no error
in the line(s) before) or not (there was an error)

On Error GoTo 0
This will enable the standard error handling (abort program, report error info) and
clear the Err object

If iErr <> 0 Then
     This branch will be entered when iErr is not 0; meaning that there was an error
     before (the info about this error is lost because of the OEG0)
...
     objFile.WriteLine "Script Error: " & Now
     No problem writing this info to the log file
...
     objFile.WriteLine "Error: " & Err.Number
     Writing the info from the cleared Err object to the log file - pretty senseless
...

Considering the links to the discussion of VBScript Error Handling

  http://www.microsoft.com/technet/scriptcenter/guide/sas_vbs_lfia.mspx?mfr=true

        "Requires extra work on the part of the script writer. The script writer must anticipate
         where errors are likely to occur and then include the code to respond appropriately."

  http://www.microsoft.com/technet/scriptcenter/resources/begin/ss0506.mspx

       "The way the Err object works is that, when an error occurs, the Err object contains all the
         information about that error. Think of the Err object as a basket. We’ve put something into
         the basket. But the next time we go through our For loop to check for the next date in the
         array, our basket is still full, which means the Err object still contains the error from last time.
         So you always want to “empty the basket,” or clear the Err object, before you run another
         method, otherwise you might end up with a full basket when you’re expecting an empty one.
         And just in case our wonderful basket metaphor wasn’t crystal clear, look what happens when
         we don’t call Err.Clear"

  http://www.microsoft.com/technet/scriptcenter/resources/scriptshop/shop1205.mspx

         "VBScript error-handling requires two elements that work together. You can turn it on with the
          On Error Resume Next statement and turn it off with On Error GoTo 0. When it's turned on you
          can use the built-in Err object to get some information on what kind of error occurred."

and the sample code from:

   http://www.visualbasicscript.com/m_43706/tm.htm

           nErrNumber = Err.Number               ' Copy the Err info (cleared by OEG0) to
           sErrDescription = Err.Description    ' two simple variables
           On Error Goto 0
           If 0 <> nErrNumber Then
               objFile.WriteLine nErrNumber

nothing of the above should surprise you. So please answer:

I take it that this just means that the scripts has completed without any errors?

yourself.


ehvbs

(in reply to morpheus83uk)
 
 
Post #: 2
 
 RE: Error Message - 3/28/2007 10:23:31 AM   
  morpheus83uk

 

Posts: 305
Score: 0
Joined: 8/21/2006
Status: offline
I am really sorry for being stupid and tell me to refer to the documents if I have to I will do...

I have read some of them and I have modified my code and I have searched google...

The exit code I am getting I cant find an explination on google for it... it appears that its just the script completing... however I could be wrong...

I am still learning loads from these forums..

I have taken out the on error go to 0 thing as it clears the error and I have just left the on error resume next in there so if it errors it should in theory write it to the text file however it seems to work but I dont know if it is properly working or not...

Based on the info from your post and the example you have given me...

I take it that the code supplied to me was what I needed and it was all correct and working and I needed not to change it...

So if I understand correctly..

          nErrNumber = Err.Number               ' Copy the Err info (cleared by OEG0) to
          sErrDescription = Err.Description    ' two simple variables
          On Error Goto 0
          If 0 <> nErrNumber Then
              objFile.WriteLine nErrNumber

The first part of the code above get the error information when an error occures..

          nErrNumber = Err.Number 
          sErrDescription = Err.Description 

So code errors at some point and that above gets the error number and the description....

The next part will then clear the error and pop it up on screen (If OERN is not in play).

On Error Goto 0

So then the next part of it :

  If 0 <> nErrNumber Then
              objFile.WriteLine nErrNumber

Would be using the copied information before it was cleared and then thus writing it into the file....

Am I correct or miles off? Bear in mind I am trying to learn about this and at the same time get my head around what its doing...

Many Thanks

James

(in reply to ehvbs)
 
 
Post #: 3
 
 RE: Error Message - 3/28/2007 7:32:27 PM   
  ehvbs

 

Posts: 2106
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi morpheus83uk,

I don't think you are stupid; there is no need to apologize. But you are
making it difficult for yourself to learn and for me to help you. It's
quite normal to be still in doubt after reading Docs from Microsoft or
postings to a newsgroup or a forum. Most often it helps to try things out.
That shouldn't be done by modifying a real world script back and forth -
to many different quite heterogeneous sub problems to distract you.
Futhermore: By changing code you will loose the older versions, which
more often than not are valuable for later reference or new starts.

So start with an empty errortrials.vbs and write


      

If you run it from a DOS box like this:

cscript errortrials.vbs

the program will abort and you will get an error message like

... \errortrials.vbs(8, 3) Laufzeitfehler in Microsoft VBScript: Division durch Null

Such messages can be copied easily to the forum's editor. No problem at all to
write

The exit code I am getting is:
    Division durch Null
I can't find an explanation for that.

If you call the script like this

wscript errortrials.vbs

an error dialog pops up. If you press Ctrl-C before you press Ok, you have

   ---------------------------
   Windows Script Host
   ---------------------------
   Skript:    ... \errortrials.vbs
   Zeile:    8
   Zeichen:    3
   Fehler:    Division durch Null
   Code:    800A000B
   Quelle:     Laufzeitfehler in Microsoft VBScript

   ---------------------------
   OK
   ---------------------------

in the clipboard. No reason at all to keep that info secret if you ask for help.


      

I hope that after looking at the output:

   cscript errortrials.vbs
   10 / 1 =  10
   10 / 0 =  10

you'll take the warnings against error handling by stealth (hiding the
errors) seriously. Do you what to have your salary calculated by code
like this?

Now a person just learning VBScript may wonder: Why does the second version
of errortrials.vbs run without executing (the code in) demoStealthEH()? Such
a person my check the Docs to learn that WScript.Quit exits the program and
passes its argument (the return value of the function demoStealthEH()) to
the operating system. So the line

WScript.Quit demoStandardEH()

won't never be reached. This could be proved by switching those lines:

   WScript.Quit demoStandardEH()
   WScript.Quit demoStealthEH()

or using a disabling comment:

   ' WScript.Quit demoStealthEH()
   WScript.Quit demoStandardEH()

This version will revert to the abort behavior. Now add


      

Next step:


      

As you can see this is even worse than stealth, because it seems to do
further checking but doesn't.

Ok, last step, I promise:


      

After working thru this sample code you should know by experience:

(1) The way of proper error handling dm_4ever showed you some days ago:
     
           You put On Error Resume Next before the piece of code you know will or could
           possibly generate an error and which you are fine with it doing so. Right AFTER
           the section or line of code that could possibly generate the error you put

             If Err.Number <> 0 Then
                ' perform some action
             End If
             On Error GoTo 0

(2) The slight improvement: Keeping the OERN and OEG0 lines even closer together
     by copying the info:
    
           On Error Resume Next       <- std error handling switched off
             nNum = 10 / 0            <- just one dangerous line
             nErr = Err.Number        <- copy
             sErr = Err.Description
           On Error Goto 0            <- std error handling switched on
             If 0 <> nErr Then        <- check if error
                ' perform some action <- do what must be done in case of error
             End If

Good luck!

ehvbs           

(in reply to morpheus83uk)
 
 
Post #: 4
 
 RE: Error Message - 3/28/2007 8:56:52 PM   
  morpheus83uk

 

Posts: 305
Score: 0
Joined: 8/21/2006
Status: offline
Ahhh right I understand now...

It never copied the values onto the clipboard and then when it had to wite the error it has already been clearned... so there was an error somewhere in the script but it would not show as the OEG0 cleared it...

I see now... Its taken a while to get there but I nundrstand from your demonstrations in this post....

I think I MIGHT be able to have another pop at this on my own and see what I come up with... I am thinking it should be soming which has been posted before hand...

I will throw it in here and let you guys see it first... 

Its just I dont want it to prompt the user as it would go againt the policy of the company so hence why I was making a big deal of it.. If I understandit correctly I could get away with it without using the OERN and it should in theory work.... I will give it a go and see what happens and rerun my maintenance_quick script with what I have in mind and see what happens from there...

Many Thanks

James

(in reply to ehvbs)
 
 
Post #: 5
 
 RE: Error Message - 3/29/2007 8:37:38 AM   
  morpheus83uk

 

Posts: 305
Score: 0
Joined: 8/21/2006
Status: offline
Quick Question...

Is there anyway that you can capture the error Line and Char when using error handling?

Oh my code is as follows : (I have commented out OERN for the time being at the start of the script just for testing)


      

Also would I be correct in thinking that I would not need the err.clear there as the OEG0 is there and would clear the error for me?   Or would I be best putting the ONG0 below the IF statement?

Is my code any better do you think? How would I be able to make it error so I could test it?

Many Thanks

James

EDIT: Had another go at the code so updated new version rather than multipost

< Message edited by morpheus83uk -- 3/30/2007 2:12:14 AM >

(in reply to morpheus83uk)
 
 
Post #: 6
 
 RE: Error Message - 3/31/2007 4:23:15 AM   
  morpheus83uk

 

Posts: 305
Score: 0
Joined: 8/21/2006
Status: offline
Would anyone be able to shed any light upon this?

Would the code I have below do what I want? and also how would I capture the Char and Line?

Any help would be greatly appreciated..


      

Many Thanks

James

EDIT: Added Code



< Message edited by morpheus83uk -- 3/31/2007 4:25:26 AM >

(in reply to morpheus83uk)
 
 
Post #: 7
 
 RE: Error Message - 3/31/2007 5:06:27 PM   
  Snipah


Posts: 1343
Score: 6
Joined: 11/1/2004
From: Netherlands
Status: offline
Technet is very explicit in it's explanation of Err-handling:

quote:

The VBScript Err object is a unique kind of object that you don't have to create or get: it is instantiated automatically by VBScript when the script runs.
Err has three properties that are generally useful:
Number (the default property) - integer
Source - string
Description - string

It also has two other properties that you can ignore unless the application in question has created custom errors and tied them to help topics:
HelpFile
HelpContext

Err also provides two methods:
Clear
Raise(lngNumber, strSource, strDescription)

_____________________________

For more information, please see the "Read me First" topic.

http://www.visualbasicscript.com

(in reply to morpheus83uk)
 
 
Post #: 8
 
 RE: Error Message - 3/31/2007 11:53:57 PM   
  morpheus83uk

 

Posts: 305
Score: 0
Joined: 8/21/2006
Status: offline
Cool thanks for that!! :)

I appreaciate the info :)

Does my code look correct I have been trying diffrent things and I think I have cracked it... Would anyone care to comment?

Many Thanks

James

(in reply to Snipah)
 
 
Post #: 9
 
 RE: Error Message - 4/1/2007 12:10:23 AM   
  Snipah


Posts: 1343
Score: 6
Joined: 11/1/2004
From: Netherlands
Status: offline
Your code looks as correct as you like....

Here are some pointers:

1) make sure it works!
2) make sure you know where what is (in case of troubleshooting)
3) comment comment comment prior to any important piece of actioncode. Comment as much as that a second objective person can understand your script.

_____________________________

For more information, please see the "Read me First" topic.

http://www.visualbasicscript.com

(in reply to morpheus83uk)
 
 
Post #: 10
 
 RE: Error Message - 4/1/2007 12:45:31 AM   
  morpheus83uk

 

Posts: 305
Score: 0
Joined: 8/21/2006
Status: offline
Will do thanks :)

I appreciate all the help everyone has given me other time I have been trying to sort my script out and also the fact of everyone pointing me in the correct direction to learning resources as well I have learned alot and achieved so much!!

Thanks again!!   

James

(in reply to Snipah)
 
 
Post #: 11
 
 RE: Error Message - 4/1/2007 8:33:52 PM   
  ehvbs

 

Posts: 2106
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi morpheus83uk,

I seriously doubt that your question

  Would the code I have below do what I want?

can be answered with "yes". But as you provide no context (where is the
line you suspect of failing?), I can only say: If you want to write the error info
to

   "C:\Program Files\Maintenance\Maintenance_Error_Log.txt"

then you should use oTS to output it. If you opened objFile before, then
opening it again is an error - probably masked/hidden some OERN in the
code you didnt post.

As to your

   how would I capture the Char and Line?


the sort answer is: you can't.

ehvbs

(in reply to morpheus83uk)
 
 
Post #: 12
 
 RE: Error Message - 4/2/2007 2:34:34 AM   
  morpheus83uk

 

Posts: 305
Score: 0
Joined: 8/21/2006
Status: offline
Hello,

Thanks for your post..

I am not entierly sure I understand though...

It will error if ObjFile is already opened (Opened previously in the script) I understand this..

I dont get the part about the using oTS to output to it...

I have tried putting thinks like ots.writeline etc but it wont have any of it... What would I need to do to output to it with the oTS??

"probably masked/hidden some OERN in the code you didnt post. "

I dont understand that as I have commented out the OERN for testing....  or do you mean there is some more in there?

Many Thanks

James

(in reply to ehvbs)
 
 
Post #: 13
 
 RE: Error Message - 4/2/2007 3:17:31 AM   
  morpheus83uk

 

Posts: 305
Score: 0
Joined: 8/21/2006
Status: offline
Sorry I think I understand what you mean (Well part of it anyways!)

Do you mean the code should look more like:


      

Cheers

James

(in reply to morpheus83uk)
 
 
Post #: 14
 
 RE: Error Message - 4/7/2007 10:13:59 PM   
  morpheus83uk

 

Posts: 305
Score: 0
Joined: 8/21/2006
Status: offline
Hello,

I am still getting the error of 0 and I am not sure why... would I need to remove the OEG0?

Or am I way of course...

Many Thanks

James 

(in reply to morpheus83uk)
 
 
Post #: 15
 
 RE: Error Message - 4/9/2007 1:18:33 AM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
I think enough time has passed and you have probably made enough changes to your code that it would be good to have a quick reset. COuld you post the actual code that you are running along with a clear description of theproblem that you are having?

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to morpheus83uk)
 
 
Post #: 16
 
 RE: Error Message - 4/9/2007 1:29:04 AM   
  morpheus83uk

 

Posts: 305
Score: 0
Joined: 8/21/2006
Status: offline
Hello,

The code I have is:


      

The problem is whenever an error in my script occures I get the error of 0 and the description of nothing... It does not matter what the error is but if I take out the ON Error Resume Next it will pop up the error... It will NOT write it into the next file like I would like.

So if the script errors... it should write the error information into the Maintenance_Error_Log.txt file for us to look into at a later date.

I hope this make sense?

Many Thanks

James

(in reply to ebgreen)
 
 
Post #: 17
 
 RE: Error Message - 4/9/2007 1:35:58 AM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
Part of the problem is that you are only checking the value of Err.Number at the very end of your script. Every action that could generate an error resets Err.Number to the value of the error. This means that if you have an action that fails and immediately after that you have an action that is succeeds, the Err.Number will be 0 because it only reports the last action's error. In your case you also open the text file for writing right before you try to write out the Err.Number. So as long as the script can open the file it will always report an Err.Number of 0. What you need to do is to check the value of Err.Number after every action that you think might fail and write out to the log file at that point in the script not at the end.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to morpheus83uk)
 
 
Post #: 18
 
 RE: Error Message - 4/9/2007 3:16:23 AM