caoccao/Javet

How to capture errors in Promise in Java?

Closed this issue · 5 comments

    public static void main(String[] args) {
        try (V8Runtime v8Runtime = V8Host.getNodeInstance().createV8Runtime()) {
            v8Runtime.getExecutor("""
                async function a() {
                	bbb()
                }
                
                a()
                """).execute();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

If an error occurs, the JVM will shut down

If the content of JavaScript is not controlled

What should I do?

Thanks

You may get it by the Promise returned by that execute(), and you must call await() to pump the event loop so that V8 can actually process the async task queue.

Please refer to this doc for detail.

Thank you very much. The problem has been resolved.

Can I still know his sourceLine and resource and startColumn and endColumn for this situation, i need them

The error type I have captured now is V8ValueError

Yes, it has all you need. Please refer to this file for details.

Thank you very much for your help