[prev in list] [next in list] [prev in thread] [next in thread]
List: openjdk-nashorn-dev
Subject: Re: raise ecmascript error from JSObject
From: Attila Sari <sari.attila () gravityrd ! com>
Date: 2017-02-01 9:57:00
Message-ID: CAKpS+SYxG8ruNcBtipG5+1OgS8G43V2kwFWCHE+2ShrH7oNQJA () mail ! gmail ! com
[Download RAW message or body]
Absolutely, many thanks.
On Tue, Jan 31, 2017 at 6:03 PM, Sundararajan Athijegannathan <
sundararajan.athijegannathan@oracle.com> wrote:
> Hope the following sample code helps you...
>
> -Sundar
>
> import javax.script.*;
> import jdk.nashorn.api.scripting.*;
>
> public class Main {
> private static JSObject errorCtr;
>
> public static void main(String[] args) throws Exception {
> ScriptEngine e = new ScriptEngineManager().getEngin
> eByName("nashorn");
> // save JS Error constructor which is used later to construct Error
> object
> errorCtr = (JSObject)e.eval("Error");
> e.put("func", new MyFunc());
> e.eval("try { func() } catch(e) { print(e); print(e.stack); } ");
> }
>
> public static class MyFunc extends AbstractJSObject {
> public boolean isFunction() { return true; }
>
> public Object call(Object self, Object... args) {
> // this is like "var e = new Error('myErrorMsg')" in JS code
> JSObject error = (JSObject)errorCtr.newObject("myErrorMsg");
> // equivalent to "throw e;"
> throw (RuntimeException)error.getMember("nashornException");
>
> }
> }
> }
>
>
> On 31/01/17, 10:04 PM, Attila Sari wrote:
>
>> Hi all,
>>
>> Is it possible to throw ecmascript error from JSObject methods? (that can
>> be catched later in the script, and will be enriched with filename and
>> line
>> number)
>>
>> Thanks,
>> Attila Sari
>>
>>
--
--
Attila SARI
Software developer | Gravity R&D <http://www.gravityrd.com/>
<http://www.gravityrd.com/>
p: +36.1.799.5264 | skype: attila.sari86 | tw: @gravityrd
<http://www.twitter.com/gravityrd>
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic