Topic Options
#1318 - 11/21/03 06:36 PM Passing exceptions from VB to WC
Luke Tomasello Administrator Online   content
Member

Registered: 09/17/00
Posts: 739
Loc: San Jose, CA., USA
Code:
## Main script - Weekly Maintenance
## test exception handling from VBScript
{
	-name "Weekly Maintenance"
	-start 
	-stop
	-action -onerror my_exception_handler
	
	worker()
	Print("SHOULD NOT GET HERE")
}
	
'' VBScript to do whatever
Function worker()
	' start out in a good state
	worker = "all is well"
	
	' create an error condition
	error = true
	
	' test for an error
	If error = True Then	
	
		' use the function return to carry the error string
		worker = "Boo, something BAD happened!"
	
		' raise the exception in the calling script
		WCCtrl.Raise()
	
	End If
End Function
	
## Exception handler
{
	-name my_exception_handler
	Print("exception handler =: '%TG.COMMAND_RETURN%'")
	-action -return stop
}

  
_________________________
Regards,
Luke Tomasello

Top
#1319 - 11/21/03 07:11 PM Re: Passing exceptions from VB to WC
Luke Tomasello Administrator Online   content
Member

Registered: 09/17/00
Posts: 739
Loc: San Jose, CA., USA
Okay, so lets talk about what's going on in the above script.

As usual, a WinCron script is the main entry point for this job. Neither the WCScript exception handler, nor the VBScript ‘worker’ execute on their own and must be called to run.

On a historical note; once every second, WinCron looks for jobs with a -start trigger. When it finds one, it evaluates the -start condition and runs the script if appropriate.

So after WinCron decides to run our job "Weekly Maintenance", it begins executing the script statements.
When WinCron encounters the function call worker(), WinCron asks VBScript to takeover execution. WinCron then waits for the VBScript function to finish before continuing execution.

Now, there are 3 interesting lines in this job.
1. -action -onerror my_exception_handler
2. WCCtrl.Raise()
3. -action -return stop

The “-action -onerror my_exception_handler” is found in the body of the "Weekly Maintenance" job and tells WinCron what to do in case there is an error or exception. In this case WinCron is instructed to run the exception handler script my_exception_handler.
(Currently only WCScript procedures may be exception handlers.)

Next we notice WCCtrl.Raise(). WCCtrl.Raise() will “raise an exception” that can be seen by WinCron.
Keep in mind; things that are happening within the VBScript interpreter are not visible to WinCron by default. If WinCron is to know about an exception, we need to manually raise an exception with WCCtrl.Raise().

And finally we see “-action -return stop” . This directive is found in our exception handler and instructs WinCron how to proceed. In this instance, we are telling WinCron to “stop”.
_________________________
Regards,
Luke Tomasello

Top


Moderator:  Luke Tomasello