Catching a C++ exception that is thrown from within native function

149 views
Skip to first unread message

Rich Eakin

unread,
Jan 22, 2015, 4:51:13 AM1/22/15
to mi...@dartlang.org, vm-...@dartlang.org
First, apologies for double posting, I really still don't know what the correct place is to ask questions like these..

Using the embedder's api, I'm finding that if I cannot catch a C++ exception that is thrown (by me) from within a native callback - to the system it appears as an uncaught exception and the application terminates, even when the Dart_Invoke() call that led to produced the native callback is wrapped in a try/catch block.  Is this expected?

I was hoping to be able to throw exceptions and exit the script loading early (I use RAII for entering/exiting scope, etc), but this won't work if I can't catch the exception and fail gracefully.  Is there a different recommended way to report an error and finish the current script?

Thanks,
Rich  

William Hesse

unread,
Jan 22, 2015, 8:55:20 AM1/22/15
to General Dart Discussion, vm-...@dartlang.org
To throw an exception into Dart, you need to use the Dart embedding API to throw a new Dart exception.

I would put a try-catch in your C++ code, around the code that might throw, and where the C++ exception is caught, you need to:

1) Create the object you want to throw with Dart_New() or some other constructor that returns a Dart_Handle to an object.
2) Throw that object with Dart_ThrowException().

Good examples of doing this are in runtime/bin/dartutils.cc and the ThrowPRException function in runtime/bin/secure_socket.cc.

OSError in those functions is just a type used by the dartutils library to add additional information about actual OS errors to a Dart exception  - you don't need it in the objects
you throw.




--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.



--
William Hesse

Rich Eakin

unread,
Jan 24, 2015, 1:12:46 AM1/24/15
to vm-...@dartlang.org, mi...@dartlang.org
Ah, I see!  I didn't realize I had to throw an exception into dart, and then I could handle this error on the C++ side later, when I get an error Dart_Handle back from Dart_Invoke(). Brilliant, this works perfectly.

Out of curiosity, is the reason why I can't naturally catch a C++ exception that I throw during a script invocation due to the fact that the dart code is being JIT compiled?

Thanks for the help,
Rich
Reply all
Reply to author
Forward
0 new messages