cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
hlazar
Level III

try() and throw()... I just do not understand

I have really tried (no pun intended) to use try() and throw() but it does not seem to work the way I read it.

I have a data set with columns Value and Parameter. I want to run a script and do many things to this data set but only if those columns exist. Can someone explain to me how to use try() and throw() in these instances or if they should be used at all?

Should the script look like this?

mydt=current data table();
if (
   try(
      Column (mydt, "Value") & Column (mydt, "Parameter"),
      throw("Value and Parameter columns do not exist");
   );
   rest of script;
);

 

 

or this?

mydt=current data table();
try(
   Column (mydt, "Value") & Column (mydt, "Parameter");
   rest of script,
   throw("Value and Parameter columns do not exist");

);

 

2 ACCEPTED SOLUTIONS

Accepted Solutions
txnelson
Super User

Re: try() and throw()... I just do not understand

I think what you really want to do is:

If(
	Try( Column( mydt, "Value" ) << get name, "" ) == "" | 
	Try( Column( mydt, "Parameter" ) << get name, "" ) == "",
	Throw( "Value and Parameter columns do not exist" ), 
   
	rest of script
);

The Try function lets you perform an operation, that if it fails it will not stop processing.  It also allows for a second paramater to be set, which will be what is returned if the Try finds a fail condition.

So the above script will return a "" if the variable "value" does not exist, and will return a "" if the variable "Parameter" does not exist and since an OR "|" clause is used, the result of the overall comparison will be true if either variable is not found.  If the resut is true, the Throw() function will be executed, otherwise the rest of the code will be executed.

 

Jim

View solution in original post

Re: try() and throw()... I just do not understand

The Trow() function, when used in this manner, will stop script execution and write the quoted string to the log. Additionally, if Throw() is generated from a Window action, an alert window will also appear with the message.

 

If you wanted to stop the execution of the script when those columns do not exist, you can use the Throw() function to do so. You can either include the rest of the script within the Try or after the try, either way, the script will stop execution when an error occurs.

 

Also, using the section of your code below will fail even if the columns do exist since you cannot AND (&) columns together to return anything. Error message is "Cannot convert argument to a number [or matrix] in access or evaluation of 'And'"

Column (mydt, "Value") & Column (mydt, "Parameter");

 

This example below will print "rest of script" to the log when Big Class table is opened and will throw a message to the log if it is not. I have just included references to both columns to test that the exist. 

Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA\Big Class.jmp" );

Try(
	Column( dt, "height" );
	Column( dt, "weight" );
,
	Throw( "height and weight columns do not exist" )
);

Print( "rest of script" );

 

Justin

View solution in original post

5 REPLIES 5
txnelson
Super User

Re: try() and throw()... I just do not understand

I think what you really want to do is:

If(
	Try( Column( mydt, "Value" ) << get name, "" ) == "" | 
	Try( Column( mydt, "Parameter" ) << get name, "" ) == "",
	Throw( "Value and Parameter columns do not exist" ), 
   
	rest of script
);

The Try function lets you perform an operation, that if it fails it will not stop processing.  It also allows for a second paramater to be set, which will be what is returned if the Try finds a fail condition.

So the above script will return a "" if the variable "value" does not exist, and will return a "" if the variable "Parameter" does not exist and since an OR "|" clause is used, the result of the overall comparison will be true if either variable is not found.  If the resut is true, the Throw() function will be executed, otherwise the rest of the code will be executed.

 

Jim

Re: try() and throw()... I just do not understand

The Trow() function, when used in this manner, will stop script execution and write the quoted string to the log. Additionally, if Throw() is generated from a Window action, an alert window will also appear with the message.

 

If you wanted to stop the execution of the script when those columns do not exist, you can use the Throw() function to do so. You can either include the rest of the script within the Try or after the try, either way, the script will stop execution when an error occurs.

 

Also, using the section of your code below will fail even if the columns do exist since you cannot AND (&) columns together to return anything. Error message is "Cannot convert argument to a number [or matrix] in access or evaluation of 'And'"

Column (mydt, "Value") & Column (mydt, "Parameter");

 

This example below will print "rest of script" to the log when Big Class table is opened and will throw a message to the log if it is not. I have just included references to both columns to test that the exist. 

Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA\Big Class.jmp" );

Try(
	Column( dt, "height" );
	Column( dt, "weight" );
,
	Throw( "height and weight columns do not exist" )
);

Print( "rest of script" );

 

Justin
pmroz
Super User

Re: try() and throw()... I just do not understand

Rather than use Try you can explicitly check for column existence.

dt = open("$sample_data\Big Class.jmp");

col_list = dt << get column names(string);

if (!contains(col_list, "Value") | !contains(col_list("Parameters")),
	nw = new window("Missing columns", << modal(),
		text box("Value and Parameter columns do not exist")
	);
	throw("Value and Parameter columns do not exist");
);
Conny_Wang
Level II

Re: try() and throw()... I just do not understand

your answer is very good!thank you ~ 厉害了

hlazar
Level III

Re: try() and throw()... I just do not understand

I want to thank you all. With your help, I was able to get a much better grasp on try() and throw() and have successfully used them in multiple areas of my scripts.

 

many thanks,

Heather