Skip to content

Commit 2a18210

Browse files
#59 Migrate to runtime exceptions, clean up
1 parent 40578bf commit 2a18210

File tree

10 files changed

+26
-68
lines changed

10 files changed

+26
-68
lines changed

jjava/src/main/java/org/dflib/jjava/JavaKernel.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import org.dflib.jjava.execution.MagicsSourceTransformer;
4040
import org.dflib.jjava.jupyter.Extension;
4141
import org.dflib.jjava.jupyter.kernel.BaseKernel;
42-
import org.dflib.jjava.jupyter.kernel.EvaluationException;
4342
import org.dflib.jjava.jupyter.kernel.LanguageInfo;
4443
import org.dflib.jjava.jupyter.kernel.ReplacementOptions;
4544
import org.dflib.jjava.jupyter.kernel.display.DisplayData;
@@ -212,8 +211,6 @@ public boolean autoLoadExtensions() {
212211

213212
@Override
214213
public List<String> formatError(Throwable e) {
215-
if (e instanceof EvaluationException)
216-
return formatError(e.getCause());
217214
if (e instanceof CompilationException) {
218215
return formatCompilationException((CompilationException) e);
219216
} else if (e instanceof IncompleteSourceException) {
@@ -334,21 +331,15 @@ private List<String> formatEvaluationInterruptedException(EvaluationInterruptedE
334331
return fmt;
335332
}
336333

337-
public Object evalRaw(String expr) throws Exception {
334+
public Object evalRaw(String expr) {
338335
expr = this.magicsTransformer.transformMagics(expr);
339336

340337
return this.evaluator.eval(expr);
341338
}
342339

343340
@Override
344341
public DisplayData eval(String expr) {
345-
Object result;
346-
try {
347-
result = this.evalRaw(expr);
348-
} catch (Exception e) {
349-
throw new EvaluationException(e);
350-
}
351-
342+
Object result = this.evalRaw(expr);
352343
if (result == null) {
353344
return null;
354345
}

jjava/src/main/java/org/dflib/jjava/execution/CodeEvaluator.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ private SourceCodeAnalysis.CompletionInfo analyzeCompletion(String source) {
7979
return this.sourceAnalyzer.analyzeCompletion(source);
8080
}
8181

82-
private void init() throws Exception {
82+
private void init() {
8383
for (String script : this.startupScripts)
8484
eval(script);
8585

8686
this.startupScripts.clear();
8787
}
8888

89-
protected Object evalSingle(String code) throws Exception {
89+
protected Object evalSingle(String code) {
9090
JJavaExecutionControl executionControl =
9191
this.executionControlProvider.getRegisteredControlByID(this.executionControlID);
9292

@@ -139,11 +139,11 @@ protected Object evalSingle(String code) throws Exception {
139139
case JJavaExecutionControl.EXECUTION_INTERRUPTED_NAME:
140140
throw new EvaluationInterruptedException(code.trim());
141141
default:
142-
throw e;
142+
throw new RuntimeException(e);
143143
}
144144
}
145145

146-
throw e;
146+
throw new RuntimeException(e);
147147
}
148148

149149
if (!event.status().isDefined())
@@ -154,7 +154,7 @@ protected Object evalSingle(String code) throws Exception {
154154
return result;
155155
}
156156

157-
public Object eval(String code) throws Exception {
157+
public Object eval(String code) {
158158
// The init() method runs some code in the shell to initialize the environment. As such
159159
// it is deferred until the first user requested evaluation to cleanly return errors when
160160
// they happen.
@@ -178,7 +178,7 @@ public Object eval(String code) throws Exception {
178178
/**
179179
* Try to clean up information linked to a code snippet and the snippet itself
180180
*/
181-
private void dropSnippet(Snippet snippet) throws Exception {
181+
private void dropSnippet(Snippet snippet) {
182182
JJavaExecutionControl executionControl =
183183
this.executionControlProvider.getRegisteredControlByID(this.executionControlID);
184184
this.shell.drop(snippet);

jjava/src/main/java/org/dflib/jjava/execution/CompilationException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
import jdk.jshell.SnippetEvent;
2727

28-
public class CompilationException extends Exception {
28+
public class CompilationException extends RuntimeException {
2929
private final SnippetEvent badSnippetCompilation;
3030

3131
public CompilationException(SnippetEvent badSnippetCompilation) {

jjava/src/main/java/org/dflib/jjava/execution/EvaluationInterruptedException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424
package org.dflib.jjava.execution;
2525

26-
public class EvaluationInterruptedException extends Exception {
26+
public class EvaluationInterruptedException extends RuntimeException {
2727
private final String source;
2828

2929
public EvaluationInterruptedException(String source) {

jjava/src/main/java/org/dflib/jjava/execution/EvaluationTimeoutException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
import java.util.concurrent.TimeUnit;
2727

28-
public class EvaluationTimeoutException extends Exception {
28+
public class EvaluationTimeoutException extends RuntimeException {
2929
private final long duration;
3030
private final TimeUnit unit;
3131
private final String source;

jjava/src/main/java/org/dflib/jjava/execution/IncompleteSourceException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424
package org.dflib.jjava.execution;
2525

26-
public class IncompleteSourceException extends Exception {
26+
public class IncompleteSourceException extends RuntimeException {
2727
private final String source;
2828

2929
public IncompleteSourceException(String source) {

jjava/src/main/java/org/dflib/jjava/execution/JJavaExecutionControl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public Object takeResult(String key) {
103103
return result == NULL ? null : result;
104104
}
105105

106-
private Object execute(String key, Method doitMethod) throws TimeoutException, Exception {
106+
private Object execute(String key, Method doitMethod) throws Exception {
107107
Future<Object> runningTask = this.executor.submit(() -> doitMethod.invoke(null));
108108

109109
this.running.put(key, runningTask);
@@ -137,7 +137,7 @@ private Object execute(String key, Method doitMethod) throws TimeoutException, E
137137
else if (cause instanceof SPIResolutionException)
138138
throw new ResolutionException(((SPIResolutionException) cause).id(), cause.getStackTrace());
139139
else
140-
throw new UserException(String.valueOf(cause.getMessage()), String.valueOf(cause.getClass().getName()), cause.getStackTrace());
140+
throw new UserException(String.valueOf(cause.getMessage()), cause.getClass().getName(), cause.getStackTrace());
141141
} catch (TimeoutException e) {
142142
throw new UserException(
143143
String.format("Execution timed out after configured timeout of %d %s.", this.timeoutTime, this.timeoutUnit.toString().toLowerCase()),
@@ -199,7 +199,7 @@ protected Class<?> findClass(String name) throws ClassNotFoundException {
199199
return loaderDelegate.findClass(name);
200200
}
201201

202-
void unloadClass(String className) throws ClassNotFoundException {
202+
void unloadClass(String className) {
203203
this.loaderDelegate.unloadClass(className);
204204
}
205205

jjava/src/test/java/org/dflib/jjava/JavaKernelTest.java

Lines changed: 0 additions & 22 deletions
This file was deleted.

jupyter-jvm-basekernel/src/main/java/org/dflib/jjava/jupyter/kernel/BaseKernel.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@
5959

6060
public abstract class BaseKernel {
6161
protected final AtomicInteger executionCount = new AtomicInteger(1);
62-
protected static final Map<String, String> KERNEL_META = ((Supplier<Map<String, String>>) () -> {
62+
protected static final Map<String, String> KERNEL_META;
63+
64+
static {
6365
Map<String, String> meta = null;
6466

6567
InputStream metaStream = BaseKernel.class.getClassLoader().getResourceAsStream("kernel-metadata.json");
@@ -83,8 +85,8 @@ public abstract class BaseKernel {
8385
meta.put("project", "unknown");
8486
}
8587

86-
return meta;
87-
}).get();
88+
KERNEL_META = meta;
89+
}
8890

8991
private final JupyterIO io;
9092
private boolean shouldReplaceStdStreams;
@@ -181,8 +183,6 @@ public HistoryManager getHistoryManager() {
181183
*
182184
* @return A {@link DisplayData} object containing the evaluation result in one or more
183185
* MIME formats, or null if there is no displayable result
184-
*
185-
* @throws EvaluationException If an error occurs during expression evaluation
186186
*/
187187
public abstract DisplayData eval(String expr);
188188

@@ -201,10 +201,9 @@ public HistoryManager getHistoryManager() {
201201
*
202202
* @return an output bundle for displaying the documentation or null if nothing is found
203203
*
204-
* @throws Exception if the code cannot be inspected for some reason (such as it not
205-
* compiling)
204+
* @throws RuntimeException if the code cannot be inspected for some reason (such as it not compiling)
206205
*/
207-
public DisplayData inspect(String code, int at, boolean extraDetail) throws Exception {
206+
public DisplayData inspect(String code, int at, boolean extraDetail) {
208207
return null;
209208
}
210209

@@ -227,11 +226,11 @@ public DisplayData inspect(String code, int at, boolean extraDetail) throws Exce
227226
* @return the replacement options containing a list of replacement texts and a
228227
* source range to overwrite with a user selected replacement from the list
229228
*
230-
* @throws Exception if code cannot be completed due to code compilation issues, or
231-
* similar. This should not be thrown if not replacements are available but rather just
232-
* an empty replacements returned.
229+
* @throws RuntimeException if code cannot be completed due to code compilation issues, or similar.
230+
* This should not be thrown if not replacements are available but rather just
231+
* an empty replacements returned.
233232
*/
234-
public ReplacementOptions complete(String code, int at) throws Exception {
233+
public ReplacementOptions complete(String code, int at) {
235234
return null;
236235
}
237236

jupyter-jvm-basekernel/src/main/java/org/dflib/jjava/jupyter/kernel/EvaluationException.java

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)