Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Добавлена функция std::try
  • Loading branch information
aNNiMON committed Jun 19, 2016
1 parent 23f61a6 commit 8f0f379
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
5 changes: 4 additions & 1 deletion program.own
Expand Up @@ -252,4 +252,7 @@ println formatDate(d)
println formatDate(d, newFormat("yyyy-MM-dd HH:mm:ss, EEEE"))
println parseDate("2016/05/10", newFormat("yyyy/MM/dd"))
println toTimestamp(d)
println newDate(toTimestamp(d) - 100000)
println newDate(toTimestamp(d) - 100000)

try(def() = try + 2)
println try(def() = try(), def(type, message) = sprintf("Error handled:\ntype: %s\nmessage: %s", type, message))
28 changes: 28 additions & 0 deletions src/com/annimon/ownlang/lib/modules/functions/std_try.java
@@ -0,0 +1,28 @@
package com.annimon.ownlang.lib.modules.functions;

import com.annimon.ownlang.exceptions.TypeException;
import com.annimon.ownlang.lib.*;

public final class std_try implements Function {

@Override
public Value execute(Value... args) {
Arguments.checkOrOr(1, 2, args.length);
if (args[0].type() != Types.FUNCTION) {
throw new TypeException(args[0].toString() + " is not a function");
}
try {
return ((FunctionValue) args[0]).getValue().execute();
} catch (Exception ex) {
if (args.length == 2 && args[1].type() == Types.FUNCTION) {
final String message = ex.getMessage();
final Function catchFunction = ((FunctionValue) args[1]).getValue();
return catchFunction.execute(
new StringValue(ex.getClass().getName()),
new StringValue(message == null ? "" : message));
}
return NumberValue.MINUS_ONE;
}
}

}
1 change: 1 addition & 0 deletions src/com/annimon/ownlang/lib/modules/std.java
Expand Up @@ -22,6 +22,7 @@ public void init() {
Functions.set("sleep", new std_sleep());
Functions.set("thread", new std_thread());
Functions.set("sync", new std_sync());
Functions.set("try", new std_try());

// String
Functions.set("sprintf", new std_sprintf());
Expand Down

0 comments on commit 8f0f379

Please sign in to comment.