background image
<< Exceptions - Trapping the exception number | Exceptions - Programmatically logging an error >>
Exceptions - Writing an error-handling function
<< Exceptions - Trapping the exception number | Exceptions - Programmatically logging an error >>
User's Guide
215
9 H
ANDLING
E
XCEPTIONS
Writing an error-handling function
Verify (sExpectedValue, sTestValue)
except
ErrorHandler ()
ErrorHandler ()
CALL Call
LIST OF CALL lCall
lCall = ExceptCalls ()
Print (ExceptData ())
for each Call in lCall
Print("Module: {Call.sModule}",
"Function: {Call.sFunction}",
"Line: {Call.iLine}")
Note the following about this testcase:
·
It calls the user-defined function CompValues, passing two arguments.
·
CompValues uses Verify to compare its arguments. If they are not equal,
an exception is automatically raised.
·
If an exception is raised, CompValues calls a user-defined function,
ErrorHandler, which handles the error. This is a general function that can
be used throughout your scripts to process errors the way you want.
·
ErrorHandler uses two built-in exception functions, ExceptData and
ExceptCalls.
ExceptData
All built-in exceptions have message text associated with them. ExceptData
returns that text.
ExceptCalls
ExceptCalls returns a list of the function calls that generated the exception.
You can see from ErrorHandler above, that ExceptCalls returns a
LIST OF
CALL
(
CALL
is a built-in data type that is a record with three elements:
sFunction, sModule, and iLine). ErrorHandler processes each of the calls and
prints them in the results file.
Note SilkTest also provides a function ExceptPrint, which combines
the features of ExceptCalls, ExceptData, and ExceptNum.
The results file
Running this testcase returns the following in the results file:
Testcase VerifyTest - Passed
*** Error: Verify value failed - got "yyy", expected "xxx"
Module: Function: Verify Line: 0
Module: except.t Function: CompValues Line: 121
Module: except.t Function: VerifyTest Line: 112