Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Добавлена функция std::try
- Loading branch information
Showing
3 changed files
with
33 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/com/annimon/ownlang/lib/modules/functions/std_try.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters