Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

290
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda