Exception handling in scripts?

Which hub software is the best? Where can I find script XXX? Discuss it here...(no, this is not for advertising your hub...)

Moderator: Moderators

Gaborone
Posts: 13
Joined: 2003-02-27 21:34

Exception handling in scripts?

Post by Gaborone » 2003-03-30 13:21

Hi,

Sometimes my script lose contact with my database, and I can't track why. I want to add some exception handling to my script, since i suspect that the script crashes before it manages to close the database sometimes. In the exception routine, I can then close the database, and send a message to an OP with the arguments that the crashing procedure was called, to be able to recreate the crash and find the problem.

I've seen something about "on error ... resume next" that can be used in VB, but van it also be used in VB script?

Is there another way of handling exceptions in VB sctipts?

/Gaborone

TasMan
Posts: 196
Joined: 2003-01-03 13:31
Location: Canada

Post by TasMan » 2003-03-30 14:10

Yup there is also On Error GoTo LineLabel

Code: Select all

'Example

Sub Main()
  On Error GoTo ErrHandler

  'Do things

  Exit Sub

ErrHandler:
  MsgBox "An error occured!"
End Sub
Shadows Direct Connect Hub - Taking away the light from NMDCH, leaving only shadows.....

ender
Posts: 224
Joined: 2003-01-03 22:47

Post by ender » 2003-03-30 14:52

On Errro Goto doesn't work with VBscript. You can just set On Error Resume Next, and then check err object every time a potential error could occur:

Code: Select all

a = 0
b = 0
on error resume next
c = a / b
if err.number <> 0 then
  msgbox err.description
  err.clear
end if
...

TasMan
Posts: 196
Joined: 2003-01-03 13:31
Location: Canada

Post by TasMan » 2003-03-30 14:57

Darn....I could have sworn I saw it....awww well. I suppose that's what I get for coding till 1 am in VB.
Shadows Direct Connect Hub - Taking away the light from NMDCH, leaving only shadows.....

ButterflySoul
Posts: 210
Joined: 2003-01-23 22:24
Location: Nevada

Post by ButterflySoul » 2003-03-30 18:59

You can handle each individual error number with a Select Case on Err.Number, and redirect the code from there. Like Case 0 for the code if everything went ok, Case <whatever> for an error you know might happen sometimes and know how to handle (like closing the DB) and Case Else for things you never expected to happen.

If you're not sure which error number might happen and what they stand for, add a lill

Code: Select all

colUsers.SendChatToAll cstr(sBotName), vbCrLf &"Error Number : " &Err.Number &vbCrLf &"Error Description : " &Err.Description &vbCrLf &"Error Source : " &Err.Source &vbCrLf &"Error HelpFile : " &Err.HelpFile &vbCrLf &"HelpFile Context : " &Err.HelpContext

in the script while you write/debug/test it, and try your best to have the errors happen =)
(if you want to have it give error numbers while the hub is running with users and all, just PM it to an OP/yourself instead of throwing it in the main chat, so you'll have some feedback to improve your Select Case =)

Note that it tracks only one error at a time. So if you have several places where errors can happen, you might want to have several of the "outpouts" above, followed each by an Err.Clear
[CoZ] Children of Zeus
-----
Shadows DC Hub - VBS and JS scripting at their best

Gaborone
Posts: 13
Joined: 2003-02-27 21:34

issue closed

Post by Gaborone » 2003-03-30 20:48

Thanks for all the help,

/Gaborone

Who is online

Users browsing this forum: Google [Bot] and 0 guests