do {} fail(error) {} success{} finally{}

Description

Four named code blocks that provide a way to handle errors before they can become a problem.

do

This code block will be executed for as far as no error occurs. Execution then continues to the next relevant of three (optional) blocks, even when a return statetement has been reached. This is the only block that may contain a return statement.

fail()

Will be executed right after an error occurred in the do block. Can have the error message as parameter. Execution continues to finally block if it exists.

success

Will be executed after the do block ran without error. Continues to finally block if it exists.

finally

Will always be executed last.

Example

When a robot is busy scraping web pages and downloading content, it will sometimes stumble upon invalid links, which can give runtime errors. As an example, an error like that will be caught here.

use System, Web;

var imageUrl = "http://www.xillioxillioxillio.com/thisisanimage.jpg";
do {
	// Induce an error
	var test = Web.download(imageUrl, "D:/temp/testimage.jpg");
	System.print(test);
} fail(error) {
	// Catch it
	System.print("The following error has occurred on line " :: error.line :: ": " :: error.message);
} success {
	System.print("Successfully executed the do-block");
} finally {
	// this will always be run last
	System.print("This is the finally block.");
}

will print:

The following error has occurred on line 6: Error during download
This is the finally block.

Nesting

Inside a do block it is allowed to use the same blocks as described above.