Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error Handling - on error goto Label?

Status
Not open for further replies.

MCubitt

Programmer
Mar 14, 2002
1,081
GB
Within VBS, is it possible to provide my own error handling, such:

.
.
on error goto myerrorhandle
.
.
quit

myerrorhandle:
if Err.Number=3 then
echo "Oh no, not good"
else
echo "Not so bad, but not great."
end if
quit

 
Hello MCubitt,

Vbs does not support On Error Goto Label directive. Instead, it is not difficult to implement the functionality. Something like this illustrate the idea.
Code:
set fso=createobject("scripting.filesystemobject")
on error resume next
set f=fso.opentextfile("nonexisting.txt")
errcode=err.number
err.clear
select case errcode
	case 53
                call errorhandle_53
        'case etc...
	case else
		msgbox err.number & vbcrlf & err.description
		'do something / call some routine
end select
'some other stuff
on error goto 0
'do some other stuff
set f=nothing
set fso=nothing

sub errorhandle_53	'file not found
	msgbox err.number & vbcrlf & err.description
        'do something useful
end sub
 
On VBS you can only use this syntax:
Code:
On Error Resume Next
On Error GoTo 0
The Err object is available:
Code:
On Error Resume Next
Err.Raise 3
Msg="Error# " & CStr(Err.Number) & " " & Err.Description _
 & " from " & Err.Source
WScript.Echo Msg
Err.Clear

Hope This Help
PH.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top