14.03.2014 Views

Scripting Guide - SAS

Scripting Guide - SAS

Scripting Guide - SAS

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 8 Programming Methods 215<br />

Advanced Programming Concepts<br />

Throwing and Catching Exceptions<br />

A script can stop itself by executing the Throw( ) function. If you want to escape from part of a script<br />

when it is in an error condition, you can enclose it in a Try( ) expression.<br />

Try takes two expression arguments. It starts by evaluating the first expression, and if or when the first<br />

expression throws an expression by evaluating Throw, it does the following:<br />

1. Immediately stops evaluating that first expression.<br />

2. Returns nothing<br />

3. Evaluates the second expression.<br />

Throw does not require an argument but has two types of optional arguments. If you include a<br />

character-valued expression as an argument, throwing stores that string in a global named exception_msg;<br />

this is illustrated in the first example below.<br />

Examples<br />

For example, you can use Try and Throw to escape from deep inside For loops.<br />

a = [1 2 3 , 4 5 . , 7 8 9];<br />

b = a;<br />

nr = nrow(a);<br />

nc = ncol(a);<br />

//a[2,3]=2; //uncomment this line to see the "Missing b" outcome<br />

try(<br />

sum = 0;<br />

for(i=1,i

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!