A script can stop itself by executing the Throw() function. If you want to escape from part of a script when it is in an error condition, you can enclose it in a Try() expression.
Try takes two expression arguments. It starts by evaluating the first expression, and if or when the first expression throws an exception by evaluating Throw, it does the following:
Throw does not require an argument but has two types of optional arguments. If you include a character-valued expression as an argument, throwing stores that string in a global named exception_msg; this is illustrated in the first example below.
a = [1 2 3, 4 5 ., 7 8 9];
b = a;
nr = N Row( a );
nc = N Col( a );
// a[2, 3] = 2; // uncomment this line to see the "Missing b" outcome
Try(
sum = 0;
For( i = 1, i <= nr, i++,
For( j = 1, j <= nc, j++,
za = a[i, j];
If( Is Missing( za ),
Throw( "Missing a" )
);
zb = b[j, i];
If( Is Missing( zb ),
Throw( "Missing b" )
);
sum += za * zb;
)
);
,
Show( i, j, exception_msg );
Throw();
);
i = 2;
j = 3;
exception_msg = "Missing a";
Try(
dt = Open( "Mydata.jmp" ); // a file that cannot be opened
Summarize( a = by( age ), c = count, meanHt = Mean( Height ) );
Show( a, c, meanHt );
,
Print( "This script does not work without the data set" );
Throw();
);
You do not have to use Try to make use of Throw. In this example, Throw is not caught by Try but still stops a script that cannot proceed:
dt = New Table(); // to get an empty data table
If( N Row( dt ) == 0,
Throw( "!Empty Data Table" )
);