For the latest version of JMP Help, visit JMP.com/help.


Publication date: 04/30/2021

Throw and Catch Exceptions

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:

1. Immediately stops evaluating that first expression.

2. Returns nothing

3. Evaluates the second expression.

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.

Examples

You can use Try() and Throw() to catch an exception that JMP itself throws.

Try( Throw( "Hello." ), Show( exception_msg ) );

exception_msg = "Hello.";

The following example prints a message to the log when the Try() expression cannot be executed:

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();
);

If the Throw() string begins with “!” and is inside a Try() expression, throwing creates an error message about where the exception was caught.

Try(
	Throw( "!This message has an exclamation point." );
,
	Write( "\!Nexception_msg: ", exception_msg );
	Print( "Hello from the catch block (WITH exclamation)." );
);
Print( "This AFTER message will NOT print due to error." );

You can also use Try() and Throw() to escape from deep inside For loops.

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";

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" )
);
Want more information? Have questions? Get answers in the JMP User Community (community.jmp.com).