File tree Expand file tree Collapse file tree 2 files changed +27
-6
lines changed
main/java/com/annimon/ownlang/modules/std
test/resources/modules/std Expand file tree Collapse file tree 2 files changed +27
-6
lines changed Original file line number Diff line number Diff line change @@ -14,12 +14,17 @@ public Value execute(Value... args) {
14
14
try {
15
15
return ((FunctionValue ) args [0 ]).getValue ().execute ();
16
16
} catch (Exception ex ) {
17
- if (args .length == 2 && args [1 ].type () == Types .FUNCTION ) {
18
- final String message = ex .getMessage ();
19
- final Function catchFunction = ((FunctionValue ) args [1 ]).getValue ();
20
- return catchFunction .execute (
21
- new StringValue (ex .getClass ().getName ()),
22
- new StringValue (message == null ? "" : message ));
17
+ if (args .length == 2 ) {
18
+ switch (args [1 ].type ()) {
19
+ case Types .FUNCTION :
20
+ final String message = ex .getMessage ();
21
+ final Function catchFunction = ((FunctionValue ) args [1 ]).getValue ();
22
+ return catchFunction .execute (
23
+ new StringValue (ex .getClass ().getName ()),
24
+ new StringValue (message == null ? "" : message ));
25
+ default :
26
+ return args [1 ];
27
+ }
23
28
}
24
29
return NumberValue .MINUS_ONE ;
25
30
}
Original file line number Diff line number Diff line change
1
+ use "std"
2
+
3
+ def testTryOnly() {
4
+ assertEquals(1, try(def() = 1))
5
+ assertEquals(-1, try(def() = parseInt("oops")))
6
+ }
7
+
8
+ def testCatchFunction() {
9
+ actual = try(def() = parseInt("oops"), def(class, cause) = class)
10
+ assertEquals("java.lang.NumberFormatException", actual)
11
+ }
12
+
13
+ def testCatchValue() {
14
+ actual = try(def() = parseInt("oops"), 42)
15
+ assertEquals(42, actual)
16
+ }
You can’t perform that action at this time.
0 commit comments