How to catch C++ exceptions properly #27700
|
Hi there! writing a C++ codebase and would like to know how to catch exceptions safely. it should catch the exception correctly without crashing the program. Following example(s) below shows what is expected: Example (basic pseudocode): std::vector<GFX::Image> images;
for (const std::string image_path : image_paths) {
try {
GFX::Image image(image_path); // potential point where the program could throw an exception
images.push_back(image);
} catch (std::exception &e) {
std::cerr << "Failed to load image: " << e.what() << std::endl;
}
}Expected output (e.g. desktop with GCC or clang): Actual output (e.g. Emscripten): Note the difference that it shows |
Replies: 2 comments 3 replies
|
Did you build with |
|
Exception catching is off by default, and the flag has to be present at compile and link time. Two options.
Pass it to both phases: emcc -fwasm-exceptions -c image.cpp -o image.o
emcc -fwasm-exceptions image.o -o app.jsIn CMake that means If it still doesn't catch: build with |
Exception catching is off by default, and the flag has to be present at compile and link time.
The second half is what usually bites: the build compiles fine, then links without exception
support.
Two options.
-fexceptions— Emscripten's JavaScript-based implementation. Works on every engine with Wasmsupport, noticeable size and speed cost.
-sDISABLE_EXCEPTION_CATCHING=0is the older spelling ofthe same thing.
-fwasm-exceptions— native WebAssembly exception handling. Smaller and faster, not implemented byevery engine yet; current major browsers have it.
Pass it to both phases:
In CMake that means
t…