cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
Neo
Neo
Level VI

Output data table together with error message from a function (JMP13)

My JSL function script below pulls a file from a network folder and outputs it in a data file for use elsewhere. How do I output the error message "This limit file does not exist! Check file name" together with the datafile. I am on JMP13.

 

Names Default To Here( 1 );
getLimitsFile = Function({LimFileName}, {dtspk},
dtspk = Try(
Open(
	"\\gbshyt-ppor-334e\DBX\XPD\System\Limits\"||LimFileName||".txt",
	columns(
		New Column( "ParameterName", Character, "Nominal" ),
		New Column( "ColumnLSL", Character, "Nominal" ),
		New Column( "ColumnUSL", Character, "Nominal" ),
		New Column( "ColumnUnits", Character, "Nominal" ),
		New Column( "ColumnSymb", Character, "Nominal" ),
		New Column( "ColumnComm", Character, "Nominal" )
	),
	Import Settings(
		End Of Line( CRLF, CR, LF ),
		End Of Field( Tab, Comma, CSV( 1 ) ),
		Strip Quotes( 1 ),
		Use Apostrophe as Quotation Mark( 0 ),
		Use Regional Settings( 0 ),
		Scan Whole File( 1 ),
		Treat empty columns as numeric( 0 ),
		CompressNumericColumns( 0 ),
		CompressCharacterColumns( 0 ),
		CompressAllowListCheck( 0 ),
		Labels( 1 ),
		Column Names Start( 1 ),
		Data Starts( 2 ),
		Lines To Read( "All" ),
		Year Rule( "20xx" )
	)
//,invisible)
),"This limit file does not exist! Check file name");
return(dtspk);
);

Also, in the case of an error, how to stop further execution of the code where the function is used and show the user a dialog box with the error message?

 

When it's too good to be true, it's neither
1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User

Re: Output data table together with error message from a function (JMP13)

In that case use Return() instead of Throw()

-Jarmo

View solution in original post

8 REPLIES 8
jthi
Super User

Re: Output data table together with error message from a function (JMP13)

If JMP13 provides File Exists() function you could use it with if statement. Then it is a decision what to return / how to stop program. You can use for example Throw() or Stop()

Names Default To Here(1);

getLimitsFile = Function({LimFileName}, {dtspk},
	If(!File Exists("\\gbshyt-ppor-334e\DBX\XPD\System\Limits\"||LimFileName||".txt"),
		Throw("This limit file does not exist! Check file name");
		//or use return and throw outside function
	);
	//Open...
	return(dtspk);
);
getLimitsFile("");

Show("continue...");

Edit:

Which datatable would you like to return with the error? If you cannot open the file, there is no datatable to return unless you create empty one.

-Jarmo
Neo
Neo
Level VI

Re: Output data table together with error message from a function (JMP13)

Thanks @jthi, Yes, JMP 13 does have File Exists (). I will try it.

 

I did not phrase my query correctly earlier. When the file does not exist, I just want the function to just output the error in a dialog box and stop further execution.

When the file exists, I only want the data table only and message output. How to achieve this?

When it's too good to be true, it's neither
jthi
Super User

Re: Output data table together with error message from a function (JMP13)

Do you have a dialog box outside the function which you want to fill with the message of error? And with datatable you want to return message output with the datatable?

-Jarmo
Neo
Neo
Level VI

Re: Output data table together with error message from a function (JMP13)

I do not yet a dialog box on the main code which uses the function.

When there is an error due to user passing an incorrect file name, I want the main code to show the user a dialog box with the error and stop any further execution of the main code. When there is no error in the file name, the file is pulled from the network location and passed into a data table which needs to be available in the main code. Hope I have answered your questions @jthi

When it's too good to be true, it's neither
jthi
Super User

Re: Output data table together with error message from a function (JMP13)

Sure, those should be enough! This will be a bit application dependent (and programmer/scripter) how you want to handle the error.

 

The example below has two fairly simple options how you could handle this. If JMP13 behaves similarly to JMP16 these both should be fine (of course it depends also on your application). The difference is that first one will throw an error inside the function, second one will return string if there is problem with the file path and the result is handled outside the function.

Names Default To Here(1);

open_file_with_throw = function({file_path}, {default local},
	If(!File Exists(file_path), //! inverts the File Exists result
		Throw(Eval Insert("^file_path^ file doesn't exist, check file path"));
	);
	dt = Open(file_path);
	return(dt); //return datatable reference
);

open_file_with_throw("testpath");
Show("Shouldn't be executed, due to throw");

//with working file path you will get reference to opened table returned
dt = open_file_with_throw("$SAMPLE_DATA/Big Class.jmp");

open_file_with_return = function({file_path}, {default local},
	If(!File Exists(file_path), //! inverts the File Exists result
		return(Eval Insert("^file_path^ file doesn't exist, check file path"));
	);
	dt = Open(file_path);
	return(dt); //return datatable reference
);

// this wont error, so you will have to handle error yourself
// one thing I don't like about this solution, is that you will have two different types of returns
// from same function
result = open_file_with_return("testpath");
If(Type(result) == "String",
	//Throw(result);
	//or you can build the error with modal window and stop after it is closed
	//@craige_hales post has quite good template for this type of modals here: https://community.jmp.com/t5/Uncharted/Modal-Dialogs/ba-p/436177
	//below is very simple one
	New window("Error", << modal,
		text box(result)
	);
	stop();
);
Show("Shouldn't be executed, due to stop");
dt = open_file_with_throw("$SAMPLE_DATA/Big Class.jmp");

-Jarmo
Neo
Neo
Level VI

Re: Output data table together with error message from a function (JMP13)

Thanks @jthi. How to catch the throw message in a dialog box? Below does not seem to work.

Names Default To Here(1);

open_file_with_throw = function({file_path}, {default local},
	If(!File Exists(file_path), //! inverts the File Exists result
		Throw(Eval Insert("^file_path^ file doesn't exist, check file path"));
	);
	dt = Open(file_path);
	return(dt); //return datatable reference
);

result = open_file_with_throw("testpath");
Show(result);

If(Type(result) == "String",
	New window("Error", << modal,
		text box(result)
	);
	stop();
);

 

When it's too good to be true, it's neither
jthi
Super User

Re: Output data table together with error message from a function (JMP13)

In that case use Return() instead of Throw()

-Jarmo
Craige_Hales
Super User

Re: Output data table together with error message from a function (JMP13)

I prefer the throw because it can be caught if needed, and stops the JSL if it isn't caught.

open_file_with_throw = Function( {file_path},
	{default local},
	If( !File Exists( file_path ), //! inverts the File Exists result
		Throw( Eval Insert( "^file_path^ file doesn't exist, check file path" ) )
	);
	dt = Open( file_path );
	Return( dt ); //return datatable reference
);

moreJSL = Function( {dt},
	Show( N Rows( dt ) );
	Close( dt, nosave );
);

errorHandler = Function( {msg}, Show( msg ) );

Write( "\!ncase 1:" );
Try(
	dt = open_file_with_throw( "$sample_data/big class.jmp" ),
	dt = Empty();
	errorHandler( exception_msg );
);
If( !Is Empty( dt ),
	moreJSL( dt )
);

Write( "\!ncase 2:" );
Try(
	dt = open_file_with_throw( "testpath" ),
	dt = Empty();
	errorHandler( exception_msg );
);
If( !Is Empty( dt ),
	moreJSL( dt )
);

// or even this, but the error handler may be handling exceptions from moreJSL() as well as open_file_with_throw()
Write( "\!ncase 3:" );
Try( moreJSL( open_file_with_throw( "$sample_data/big class.jmp" ) ), errorHandler( exception_msg ) );

Write( "\!ncase 4:" );
Try( moreJSL( open_file_with_throw( "testpath" ) ), errorHandler( exception_msg ) );
Craige