Tabella dei dati di output insieme al messaggio di errore da una funzione (JMP13)Il mio script della funzione JSL di seguito estrae un file da una cartella di rete e lo emette in un file di dati per l'uso altrove. Come si genera il messaggio di errore "Questo file limite non esiste! Verifica nome file" insieme al file di dati. Sono su 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);
);Inoltre, in caso di errore, come interrompere l'ulteriore esecuzione del codice in cui viene utilizzata la funzione e mostrare all'utente una finestra di dialogo con il messaggio di errore?
SceneggiaturaRe: tabella dati di output insieme al messaggio di errore da una funzione (JMP13)Grazie @jthi , Sì, JMP 13 ha File esiste (). Lo proverò.
Non ho formulato correttamente la mia domanda in precedenza. Quando il file non esiste, voglio solo che la funzione emetta l'errore in una finestra di dialogo e interrompa l'ulteriore esecuzione.
Quando il file esiste, voglio solo la tabella dati e l'output del messaggio. Come raggiungere questo obiettivo?
Re: tabella dati di output insieme al messaggio di errore da una funzione (JMP13)Se JMP13 fornisce la funzione File Exists(), è possibile utilizzarla con l'istruzione if. Quindi è una decisione su cosa restituire / come interrompere il programma. Puoi usare ad esempio Throw() o 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...");
Modificare:
Quale datatable vorresti restituire con l'errore? Se non riesci ad aprire il file, non c'è nessun datatable da restituire a meno che tu non ne crei uno vuoto.
Re: tabella dati di output insieme al messaggio di errore da una funzione (JMP13)Non ho ancora una finestra di dialogo sul codice principale che utilizza la funzione.
Quando si verifica un errore dovuto all'utente che ha passato un nome file errato, voglio che il codice principale mostri all'utente una finestra di dialogo con l'errore e interrompa qualsiasi ulteriore esecuzione del codice principale. Quando non ci sono errori nel nome del file, il file viene estratto dal percorso di rete e passato a una tabella di dati che deve essere disponibile nel codice principale. Spero di aver risposto alle tue domande @jthi .
Oggetto: tabella dati di output insieme al messaggio di errore di una funzione (JMP13)Certo, dovrebbero bastare! Questo dipenderà un po' dall'applicazione (e dal programmatore/scripter) su come vuoi gestire l'errore.
L'esempio seguente ha due opzioni abbastanza semplici su come gestirlo. Se JMP13 si comporta in modo simile a JMP16, entrambi dovrebbero andare bene (ovviamente dipende anche dalla tua applicazione). La differenza è che il primo genererà un errore all'interno della funzione, il secondo restituirà una stringa se c'è un problema con il percorso del file e il risultato viene gestito al di fuori della funzione.
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");
Oggetto: tabella dati di output insieme al messaggio di errore di una funzione (JMP13)Grazie @jthi . Come catturare il gettare messaggio in una finestra di dialogo? Di seguito non sembra funzionare.
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();
);
Oggetto: tabella dati di output insieme al messaggio di errore di una funzione (JMP13)In tal caso usa Return() invece di Throw()
Oggetto: tabella dati di output insieme al messaggio di errore di una funzione (JMP13)Preferisco il lancio perché può essere catturato se necessario e interrompe il JSL se non viene catturato.
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 ) );