Skip to content

throw exception with message in cpp #6330

Closed

Description

@robfors

I have a cpp file test.cpp:

#include <stdio.h>
#include <iostream>
#include <emscripten/bind.h>
#include <emscripten/val.h>

int main()
{
  throw std::invalid_argument("test error");
  
  printf("\ndone\n");
  return 0;
}

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.

Activity

  1. changed the title [-]throw exception in cpp[/-] [+]throw exception with message in cpp[/+] on Mar 12, 2018
  2. kripken commented on Mar 13, 2018

    @kripken
    Member

    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.

  3. jleni commented on Apr 16, 2018

    @jleni

    @kripken so there is no way of propagating the exception to js? Is there any way in which it could be wrapped or something?

  4. kripken commented on Apr 16, 2018

    @kripken
    Member

    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';
      }
    }
    

    I think that might work, but I'm not sure.

  5. jleni commented on Apr 16, 2018

    @jleni

    ok, I didnt know that was an actual pointer!
    I will play a bit with the idea and write back, thanks!

  6. added a commit that references this issue on Oct 30, 2018
  7. tuananh commented on Aug 14, 2019

    @tuananh

    is it still open? i would like to know how to throw exception from c++ to js as well.

  8. Shachlan commented on Dec 22, 2019

    @Shachlan
    Contributor

    Hi, this is the current solution I use:

    In C++:

    namespace foo {
    std::string getExceptionMessage(int exceptionPtr) {
      return std::string(reinterpret_cast<std::exception *>(exceptionPtr)->what());
    }
    }
    
    EMSCRIPTEN_BINDINGS(fooBinding) {
    emscripten::function("getExceptionMessage", &foo::getExceptionMessage);
    };

    In JS:

    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.

  9. artemjackson commented on Apr 29, 2020

    @artemjackson

    Any updates?

  10. kripken commented on Apr 29, 2020

    @kripken
    Member

    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.

  11. Shachlan commented on Apr 30, 2020

    @Shachlan
    Contributor

    @kripken, sure. I'm not well versed in the Emscripten project organization - where would you prefer something like this to be added?

  12. kripken commented on May 1, 2020

    @kripken
    Member

    @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?

  13. 47 remaining items

  14. kripken commented on Sep 17, 2021

    @kripken
    Member

    Here is where asyncify adds wrappers for all exports:

    instrumentWasmExports: function(exports) {

  15. RReverser commented on Sep 17, 2021

    @RReverser
    Collaborator

    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?

  16. sbc100 commented on Sep 17, 2021

    @sbc100
    Collaborator

    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.

  17. RReverser commented on Sep 17, 2021

    @RReverser
    Collaborator

    I don't see why embind should be special here.

    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.

  18. sbc100 commented on Sep 17, 2021

    @sbc100
    Collaborator

    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.

  19. RReverser commented on Sep 17, 2021

    @RReverser
    Collaborator

    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)?

  20. aheejin commented on Sep 17, 2021

    @aheejin
    Member

    @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.

  21. RReverser commented on Sep 17, 2021

    @RReverser
    Collaborator

    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?

  22. RReverser commented on Sep 17, 2021

    @RReverser
    Collaborator

    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.

  23. aheejin commented on Sep 17, 2021

    @aheejin
    Member

    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
    
  24. kripken commented on Sep 17, 2021

    @kripken
    Member

    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.

  25. RReverser commented on Sep 17, 2021

    @RReverser
    Collaborator

    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)

  26. added a commit that references this issue on Oct 6, 2022
    ef9c5ce
  27. added a commit that references this issue on Oct 10, 2022
    68a9f99
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions