Skip to content

Commit 8f0f379

Browse files
committed
Добавлена функция std::try
1 parent 23f61a6 commit 8f0f379

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

program.own

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,4 +252,7 @@ println formatDate(d)
252252
println formatDate(d, newFormat("yyyy-MM-dd HH:mm:ss, EEEE"))
253253
println parseDate("2016/05/10", newFormat("yyyy/MM/dd"))
254254
println toTimestamp(d)
255-
println newDate(toTimestamp(d) - 100000)
255+
println newDate(toTimestamp(d) - 100000)
256+
257+
try(def() = try + 2)
258+
println try(def() = try(), def(type, message) = sprintf("Error handled:\ntype: %s\nmessage: %s", type, message))
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.annimon.ownlang.lib.modules.functions;
2+
3+
import com.annimon.ownlang.exceptions.TypeException;
4+
import com.annimon.ownlang.lib.*;
5+
6+
public final class std_try implements Function {
7+
8+
@Override
9+
public Value execute(Value... args) {
10+
Arguments.checkOrOr(1, 2, args.length);
11+
if (args[0].type() != Types.FUNCTION) {
12+
throw new TypeException(args[0].toString() + " is not a function");
13+
}
14+
try {
15+
return ((FunctionValue) args[0]).getValue().execute();
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));
23+
}
24+
return NumberValue.MINUS_ONE;
25+
}
26+
}
27+
28+
}

src/com/annimon/ownlang/lib/modules/std.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public void init() {
2222
Functions.set("sleep", new std_sleep());
2323
Functions.set("thread", new std_thread());
2424
Functions.set("sync", new std_sync());
25+
Functions.set("try", new std_try());
2526

2627
// String
2728
Functions.set("sprintf", new std_sprintf());

0 commit comments

Comments
 (0)