1

I am writing a command line tool in haxe (a mini lisp calculator :D) at this point of time in the project I cannot prevent the user to make an invalid call to a nonexisting function or use false arguments (it would require additional wrappers), so I though on the pythonic approach: let it crash and capture the error. However It seems I cannot catch in any way a C++ segmentation fault from haxe.

Example:

hxlisp => (+ 1 2)
REPL.hx:33: Null Function Pointer
hxlisp => (hello)
make: *** [cpp] Segmentation fault: 11

How does the code look like?

public function loop() {
    while (true) {
        try {
            var inp:String = this.input();
            if (inp.length == 0) continue;
            var tree:SExpr = mkSexpr(parse(inp));
            var a = SExpr.List(sexpr_values(tree)[0]);
            var program = eval(a, env.std_env);
            this.output(program);
        } catch(eof:Eof) {
            break;
        } catch(error:Dynamic) {
            trace(error);
        }
    }
}

However it seems there is no way (or I couldn't find it) to catch the error as exception and recover from it. Does anyone have an idea about how much is possible this approach of let it crash and recover from an error in HaxeC++? Note that in Neko or javascript that is not a problem.

  • I am aware there is a related question for C++ only: http://stackoverflow.com/questions/2350489/how-to-catch-segmentation-fault-in-linux However I am looking for a haxe idiomatic solution (apart of wrap this library which doesn't look too portable). – Víctor R. Escobar Dec 12 '15 at 16:25

1 Answers1

-1

No, there is no way to catch and recover from a segmentation fault. A segmentation fault is a hard error: your process has attempted to access invalid memory and the OS is shutting the process down because of it. You must avoid segmentation faults.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • I think some OSes may send a signal to the program (`SIGSEGV` in linux)... but perhaps I am wrong or it doesn't work on all platforms. However I am mostly looking for the idiomatic haxe solution. – Víctor R. Escobar Dec 12 '15 at 16:25
  • @VíctorR.Escobar: Yes, you can hack around with signal handlers, but really [don't](https://feepingcreature.github.io/handling.html) – Lightness Races in Orbit Dec 12 '15 at 17:15