输出数据表以及来自函数的错误消息 (JMP13)下面我的 JSL 函数脚本从网络文件夹中提取文件并将其输出到数据文件中以供其他地方使用。 如何输出错误消息“此限制文件不存在! 检查文件名”连同数据文件。 我在 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);
);此外,在发生错误的情况下,如何停止使用该函数的代码的进一步执行并向用户显示带有错误消息的对话框?
脚本回复:输出数据表以及函数的错误消息(JMP13)谢谢@jthi , 是的,JMP 13 确实有 File Exists ()。 我会试试看。
我之前没有正确表达我的查询。 当文件不存在时,我只想让函数在对话框中输出错误并停止进一步执行。
当文件存在时,我只想要数据表和消息输出。 如何做到这一点?
回复:输出数据表以及函数的错误消息(JMP13)如果 JMP13 提供 File Exists() 函数,您可以将它与 if 语句一起使用。 然后决定返回什么/如何停止程序。 您可以使用例如 Throw() 或 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...");
编辑:
您希望返回哪个数据表并返回错误? 如果您无法打开文件,则除非您创建一个空的,否则没有要返回的数据表。
回复:输出数据表以及函数的错误消息(JMP13)我还没有使用该函数的主代码对话框。
当由于用户传递了不正确的文件名而出现错误时,我希望主代码向用户显示一个包含错误的对话框并停止主代码的任何进一步执行。 当文件名没有错误时,从网络位置提取文件并传递到需要在主代码中可用的数据表中。 希望我已经回答了你的问题@jthi .
回复:输出数据表以及来自函数的错误消息(JMP13)当然,这些应该足够了! 这将有点依赖于应用程序(和程序员/脚本编写者)您希望如何处理错误。
下面的示例有两个相当简单的选项,您可以如何处理这个问题。 如果 JMP13 的行为与 JMP16 相似,则两者都应该没问题(当然这也取决于您的应用程序)。 不同之处在于,第一个会在函数内部抛出错误,第二个会在文件路径有问题时返回字符串,结果在函数外部处理。
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");
回复:输出数据表以及来自函数的错误消息(JMP13)谢谢@jthi . 如何捕捉扔对话框中的消息? 下面好像不行。
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();
);
回复:输出数据表以及来自函数的错误消息(JMP13)在这种情况下使用 Return() 而不是 Throw()
回复:输出数据表以及来自函数的错误消息(JMP13)我更喜欢 throw 因为它可以在需要时被捕获,如果没有被捕获则停止 JSL。
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 ) );