Using do...except Statements to Trap and Handle Exceptions

Using do...except you can handle exceptions locally, instead of passing control to the built-in error handler, which is part of the recovery system of Silk Test Classic. The do...except statement has the following syntax:


If an exception is raised in the do clause of the statement, control is immediately passed to the except clause, instead of to the recovery system.

If no exception is raised in the do clause of the statement, control is passed to the line after the except clause. The statements in the except clause are not executed.

Consider this simple test case:

testcase except1 (STRING sExpectedVal, STRING sActualVal)

 Verify (sExpectedVal, sActualVal)
 Print ("Verification succeeded")
 Print ("Verification failed")

This test case uses the built-in function Verify, which generates an exception if its two arguments are not equivalent. In this test case, if sExpectedVal equals sActualVal, no exception is raised, Verification succeeded is printed, and the test case terminates. If the two values are not equal, Verify raises an exception, control immediately passes to the except clause, the first Print statement is not executed, and Verification failed is printed.

Here is the result if the two values "one" and "two" are passed to the test case:

Testcase except1 ("one", "two") - Passed
Verification failed

The test case passes and the recovery system is not called because you handled the error yourself.

You handle the error in the except clause. You can include any 4Test statements, so you could, for example, choose to ignore the error, write information to a separate log file, and log the error in the results file.