Writing an Error-Handling Function

If you want to customize your error processing, you will probably want to write your own error-handling function, which you can reuse in many scripts.

Open Agent Example

For example, you might want to print the text associated with the exception as well as the function calls that generated the exception. The following test case illustrates this:
testcase VerifyTest ()
   STRING sTestValue = "xxx"
   STRING sExpectedValue = "yyy"
   CompValues (sExpectedValue, sTestValue)

CompValues (STRING sExpectedValue, STRING sTestValue)
   do
      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}")
  • The test case 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.
    Except Data
    All built-in exceptions have message text associated with them. ExceptData returns that text.
    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
    • iLine
    ErrorHandler processes each of the calls and prints them in the results file.
  • Silk Test Classic also provides the function ExceptPrint, which combines the features of ExceptCalls, ExceptData, and ExceptNum.

    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

    The second line is the result of printing the information from ExceptData. The rest of the lines show the processing of the information from ExceptCalls.

    This test case passes because the error was handled locally and not re-raised.