crossbind
GitHub
TUTORIAL · STEP 7 OF 7

Exceptions

A C++ exception reaches JavaScript as an Error, so try and catch work across the boundary.

parsePercent throws std::out_of_range for a number outside 0 to 100, and std::stoi throws std::invalid_argument for text that is not a number. Both arrive in main.js as an Error.

In a WebAssembly build the message reads <type>: <what()>, and the two parts are also on error.cppType and error.cppMessage. Throwing is the binding-friendly way to report a failure: there are no status codes or out-parameters to check.

Try it
Call Module.squareRoot(-1) outside the try block and see how an uncaught exception shows up.

Where to go next

  • Quick start: crossbind in a project of your own.
  • Libraries: ready-made C and C++ libraries, from SQLite to GDAL.
  • Rust: Rust files and crates import the same way.
  • API reference: initNative(), crossbind.config.js and the command line.
  • Playground: every sample, free to change.
Runs without a compile until you edit the C++.
Console
Press Run, or Ctrl+Enter, to run main.js.
Type to search every guide page and section.
↑↓ navigate↵ openesc close