Добавлена функция std::try

This commit is contained in:
Victor 2016-06-19 13:22:17 +03:00
parent 23f61a64e5
commit 8f0f37956f
3 changed files with 33 additions and 1 deletions

View File

@ -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))

View File

@ -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;
}
}
}

View File

@ -22,6 +22,7 @@ public final class std implements Module {
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());