Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

287
Views
Emscripten: How to catch JS exception?

Emscripten 'val.h' API allows calling methods of JS objects, however, C++ try-catch won't catch JS exception. Consider this example:

#include <emscripten.h>
#include <emscripten/val.h>

void test(){
    string t = "some invalid json";
    val    v = val::object();

    // This C++ try-catch doesn't catch JS exception
    try {
        v = val::global("JSON").call<val>("parse", t);
        cout <<"ok" <<endl;
    }
    catch(...){
        cout <<"failed" <<endl;
    }

    cout <<"ret" <<endl;
}

The JS exception makes the 'test' function stop and no ok, no failed, no ret printed out. How to catch that JS exception thrown by JSON.parse?

There's 1 issue here but it's still open: https://github.com/emscripten-core/emscripten/issues/11496

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Based on documentation:

By default, exception catching is disabled in Emscripten.

You have to enable it with the -fexceptions argument.

about 4 years ago · Juan Pablo Isaza Report

0

Emscripten doesn't seem to be able to catch JS exceptions in C++ yet. Here's a work-around:

// extern_pre.js
function json_parse(str){
    try{
        return JSON.parse(str);
    }
    catch(E){
        return null;
    }
}
// app.cpp
...
val v = val::global("json_parse")(some_str);

Build:

emcc app.cpp -o app.js --bind --extern-pre-js extern_pre.js
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!