Skip to content

Commit 1f41932

Browse files
committed
ScriptREPL: catch exceptions for built-in commands
Previously, we only caught exceptions when evaluating lines of script. But the built-ins can go wrong, too; e.g., when asking a script engine to do some operation which it does not support. So let's just always catch all exceptions when talking to the script engines, to make a best effort to recover nicely whenever possible. Note that this patch is mostly indentation changes; use "git diff -b" to see the true scope of the changes more easily.
1 parent 5b3af85 commit 1f41932

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

src/main/java/org/scijava/script/ScriptREPL.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -167,37 +167,37 @@ public void prompt() {
167167
* @return False iff the REPL should exit.
168168
*/
169169
public boolean evaluate(final String line) {
170-
final String tLine = line.trim();
171-
if (tLine.equals(":help")) help();
172-
else if (tLine.equals(":vars")) vars();
173-
else if (tLine.equals(":langs")) langs();
174-
else if (tLine.startsWith(":lang ")) lang(line.substring(6).trim());
175-
else if (tLine.equals(":quit")) return false;
176-
else {
177-
// ensure that a script language is active
178-
if (interpreter == null) return true;
179-
180-
// pass the input to the current interpreter for evaluation
181-
try {
170+
try {
171+
final String tLine = line.trim();
172+
if (tLine.equals(":help")) help();
173+
else if (tLine.equals(":vars")) vars();
174+
else if (tLine.equals(":langs")) langs();
175+
else if (tLine.startsWith(":lang ")) lang(line.substring(6).trim());
176+
else if (tLine.equals(":quit")) return false;
177+
else {
178+
// ensure that a script language is active
179+
if (interpreter == null) return true;
180+
181+
// pass the input to the current interpreter for evaluation
182182
final Object result = interpreter.interpret(line);
183183
if (result != ScriptInterpreter.MORE_INPUT_PENDING) {
184184
out.println(s(result));
185185
}
186186
}
187-
catch (final ScriptException exc) {
188-
// NB: Something went wrong interpreting the line of code.
189-
// Let's just display the error message, unless we are in debug mode.
190-
if (log.isDebug()) exc.printStackTrace(out);
191-
else {
192-
final String msg = exc.getMessage();
193-
out.println(msg == null ? exc.getClass().getName() : msg);
194-
}
195-
}
196-
catch (final Throwable exc) {
197-
// NB: Something unusual went wrong. Dump the whole exception always.
198-
exc.printStackTrace(out);
187+
}
188+
catch (final ScriptException exc) {
189+
// NB: Something went wrong interpreting the line of code.
190+
// Let's just display the error message, unless we are in debug mode.
191+
if (log.isDebug()) exc.printStackTrace(out);
192+
else {
193+
final String msg = exc.getMessage();
194+
out.println(msg == null ? exc.getClass().getName() : msg);
199195
}
200196
}
197+
catch (final Throwable exc) {
198+
// NB: Something unusual went wrong. Dump the whole exception always.
199+
exc.printStackTrace(out);
200+
}
201201
return true;
202202
}
203203

0 commit comments

Comments
 (0)