You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Compiled with emcc --bind -fexceptions -std=c++11 test.cpp -o test.js
I would like the code to print out the exception message in some way so the user has a description of the error.
When I run it I get exception thrown: 5246984 and Uncaught 5246984.
It does not appear as though emscripten will print any distinguishable message.
Does emscripten have this feature?
If not, I was thinking of adding a thow method to val that would accept a message and possibly a javascript exception class.
Reacted by Juan Leni, Arseniy Fokin, Artyom Stepanishchev, Lars Melchior, Tim Straubinger and Jaroslav Louma
I don't think we have such a feature. In general I think people just add a try-catch at the highest level and print it from C++. If there's a way to add that feature it would be nice, but I'm not sure how easy it would be.
I can't think of an easy way. But the thrown exception is a pointer, so in theory you could call from JS into C++ code like this, and give it that pointer:
// gets an exception object, and prints it out.
void print_exception(void* exception) {
try {
throw exception;
} catch (...) {
std::cout << exception.what() << '\n';
}
}
try{
... // some code that calls webassembly}catch(exception){console.error(Module.getExceptionMessage(exception));}finally{
...
}
I guess you can also throw an exception from an EM_ASM block, if you wanted to rethrow c++ to JS.
Reacted by Tuan Anh Tran, Artyom Stepanishchev, Teodor Tanasoaia, Lars Melchior, earonesty, Ben Schattinger, Steven Mirabito, Jason, cnw@ngjihe, ted537 and 5 more
Reacted by oscarmampel, Artyom Stepanishchev and Ambrose Leeb
@Shachlan Good question, I'm not sure. Perhaps just adding an entry in the docs is enough, like in the debugging docs at ./site/source/docs/porting/Debugging.rst?
Here is where asyncify adds wrappers for all exports
I was thinking that we could limit it to Embind exports, since that would 1) limit wrappers to C++ code and not C where we know exceptions don't exist so we don't need to catch them and 2) limit it to explicitly Embind-exposed methods where users already expect value conversions and some associated overhead.
Do you think we should wrap every exported function instead?
My motivation is mostly because currently it's the method for exposing C++ code, and C++ code is the only place where C++ exceptions can originate, so it feels like a natural boundary / annotation for the wrapping.
I guess it's possible that some C++ code is exposed via C FFI, but in those scenarios exceptions are usually exposed via C style get_last_error functions too instead of propagating across FFI boundary as-is.
I guess that makes sense yes. I guess it would be easy to limit the wrapping to just embind exports.
OTOH, if exceptions are enabled there is no reason they can't be thrown when normal (non-embind) exports are called directly and it might be confusing for users if uncaught exceptions only sometimes have useful messages on them.
there is no reason they can't be thrown when normal (non-embind) exports are called directly
Right, that's what I meant by C FFI for C++ codebase.
Or are you saying there are users who call C++ methods directly from JS via their mangled names with no binding layer in between, like Module.__Z6squarei(10)?
@RReverser I'm not familiar with embind, but does that also take care of exceptions escaping main? Or that should be handled separately?
The other thing is, if we use SjLj support using Wasm EH (#14976), longjmps will surface as exceptions as well. Longjmps don't have a helpful message, but it'd be good to just at last print "longjmp".. But I just checked in native Linux and unmatched longjmps surface as a segmentation fault there, so it's not great there either.
but does that also take care of exceptions escaping main? Or that should be handled separately?
Yeah no, main does need to be handled separately.
The other thing is, if we use SjLj support using Wasm EH (#14976), longjmps will surface as exceptions as well. Longjmps don't have a helpful message, but it'd be good to just at last print "longjmp"..
Ah that's an interesting point, makes sense. I'm not sure how to distinguish longjmps from other exceptions though? Will they have a special class like std::exception does?
To be clear - I'm not opposed to wrapping all exports either, it just seemed wasteful to wrap C functions (including somewhat frequently called things like _malloc and _free) when all we are interested in are C++ exceptions and methods.
Plus, it's usually easier to start off with a limited surface and expand later as necessary.
However, if there are cases where we want to wrap C functions, that makes sense too.
Ah that's an interesting point, makes sense. I'm not sure how to distinguish longjmps from other exceptions though? Will they have a special class like std::exception does?
No we give that a separate tag. So if we want to distinguish it it would be something like
try
main code
catch __cpp_exception
Augment object with C++ exception info string
catch __c_longjmp
Augment object with "longjmp"
end
I think it might be ok to wrap all exports. That's simpler, and it would help users that do not use embind. A common case might be a problem with no bindings, just exported main and callbacks to render a frame etc.
just exported main and callbacks to render a frame etc.
Right, but if it's callbacks, those exceptions would propagate through main anyway, and would be wrapped too. (btw, probably something we need to account for - make sure we don't try to double-wrap an exception in such scenarios)
Activity
[-]throw exception in cpp[/-][+]throw exception with message in cpp[/+]kripken commentedon Mar 13, 2018
I don't think we have such a feature. In general I think people just add a try-catch at the highest level and print it from C++. If there's a way to add that feature it would be nice, but I'm not sure how easy it would be.
jleni commentedon Apr 16, 2018
@kripken so there is no way of propagating the exception to js? Is there any way in which it could be wrapped or something?
kripken commentedon Apr 16, 2018
I can't think of an easy way. But the thrown exception is a pointer, so in theory you could call from JS into C++ code like this, and give it that pointer:
I think that might work, but I'm not sure.
jleni commentedon Apr 16, 2018
ok, I didnt know that was an actual pointer!
I will play a bit with the idea and write back, thanks!
Adds 'throw' method to val. (#7405)
Adds 'throw' method to val. (emscripten-core#7405)
tuananh commentedon Aug 14, 2019
is it still open? i would like to know how to throw exception from c++ to js as well.
Shachlan commentedon Dec 22, 2019
Hi, this is the current solution I use:
In C++:
In JS:
I guess you can also throw an exception from an
EM_ASMblock, if you wanted to rethrow c++ to JS.artemjackson commentedon Apr 29, 2020
Any updates?
kripken commentedon Apr 29, 2020
I think adding an option like @Shachlan 's from the previous comment might be a good idea, if @Shachlan or someone else wants to submit a PR.
Shachlan commentedon Apr 30, 2020
@kripken, sure. I'm not well versed in the Emscripten project organization - where would you prefer something like this to be added?
kripken commentedon May 1, 2020
@Shachlan Good question, I'm not sure. Perhaps just adding an entry in the docs is enough, like in the debugging docs at
./site/source/docs/porting/Debugging.rst?47 remaining items
kripken commentedon Sep 17, 2021
Here is where asyncify adds wrappers for all exports:
emscripten/src/library_async.js
Line 111 in 565fb36
RReverser commentedon Sep 17, 2021
I was thinking that we could limit it to Embind exports, since that would 1) limit wrappers to C++ code and not C where we know exceptions don't exist so we don't need to catch them and 2) limit it to explicitly Embind-exposed methods where users already expect value conversions and some associated overhead.
Do you think we should wrap every exported function instead?
sbc100 commentedon Sep 17, 2021
If the wrapping is cheap I don't see why we wouldn't just wrap all explicitly exported functions. I don't see why embind should be special here.
RReverser commentedon Sep 17, 2021
My motivation is mostly because currently it's the method for exposing C++ code, and C++ code is the only place where C++ exceptions can originate, so it feels like a natural boundary / annotation for the wrapping.
I guess it's possible that some C++ code is exposed via C FFI, but in those scenarios exceptions are usually exposed via C style
get_last_errorfunctions too instead of propagating across FFI boundary as-is.sbc100 commentedon Sep 17, 2021
I guess that makes sense yes. I guess it would be easy to limit the wrapping to just embind exports.
OTOH, if exceptions are enabled there is no reason they can't be thrown when normal (non-embind) exports are called directly and it might be confusing for users if uncaught exceptions only sometimes have useful messages on them.
RReverser commentedon Sep 17, 2021
Right, that's what I meant by C FFI for C++ codebase.
Or are you saying there are users who call C++ methods directly from JS via their mangled names with no binding layer in between, like
Module.__Z6squarei(10)?aheejin commentedon Sep 17, 2021
@RReverser I'm not familiar with embind, but does that also take care of exceptions escaping
main? Or that should be handled separately?The other thing is, if we use SjLj support using Wasm EH (#14976), longjmps will surface as exceptions as well. Longjmps don't have a helpful message, but it'd be good to just at last print "longjmp".. But I just checked in native Linux and unmatched longjmps surface as a segmentation fault there, so it's not great there either.
RReverser commentedon Sep 17, 2021
Yeah no,
maindoes need to be handled separately.Ah that's an interesting point, makes sense. I'm not sure how to distinguish
longjmps from other exceptions though? Will they have a special class likestd::exceptiondoes?RReverser commentedon Sep 17, 2021
To be clear - I'm not opposed to wrapping all exports either, it just seemed wasteful to wrap C functions (including somewhat frequently called things like
_mallocand_free) when all we are interested in are C++ exceptions and methods.Plus, it's usually easier to start off with a limited surface and expand later as necessary.
However, if there are cases where we want to wrap C functions, that makes sense too.
aheejin commentedon Sep 17, 2021
No we give that a separate tag. So if we want to distinguish it it would be something like
kripken commentedon Sep 17, 2021
I think it might be ok to wrap all exports. That's simpler, and it would help users that do not use embind. A common case might be a problem with no bindings, just exported main and callbacks to render a frame etc.
RReverser commentedon Sep 17, 2021
Right, but if it's callbacks, those exceptions would propagate through
mainanyway, and would be wrapped too. (btw, probably something we need to account for - make sure we don't try to double-wrap an exception in such scenarios)[EH] Print message for uncaught exceptions
[EH] Print message for uncaught exceptions (#18003)