skystrife / procxx

A simple process management library for C++ on UNIX platforms.
MIT License
143 stars 28 forks source link

Exceptions cannot be handled deterministically #2

Closed ztdwu closed 8 years ago

ztdwu commented 8 years ago

For some reason, the exception throw by process::exec() cannot be handled intuitively, probably because the library is forking processes.

Example 1:

#include "process.h"

#include <exception>
#include <iostream>

int main() {
    procxx::process p("some-invalid-program");
    p.exec(); // throws an exception

    std::cout << "====== This current line should not be printed ======" << std::endl;
}

Output 1:

====== This current line should not be printed ======
terminate called after throwing an instance of 'procxx::process::exception'
  what():  Failed to execute child process some-invalid-program

Example 2 (this one has no output):

#include "process.h"

#include <exception>
#include <iostream>

int main() {
    try {
        procxx::process p("some-invalid-program");
        p.exec(); // throws an exception

    } catch ( procxx::process::exception &e ) {
        std::cout << "why don't i get printed? :(" << std::endl;
    }
}

Is this intended? If so, how do users go about handling the exceptions thrown in by this library?

skystrife commented 8 years ago

Sorry for the late response on this. Yes, this is broken, and I shouldn't be throwing the exception in the child anyway as that's likely to cause issues.

I think 4cb62e5 fixes this. The exception should now be able to be caught in the parent process in the event of a child exec failure.

ztdwu commented 8 years ago

Cool, looks like that fixed it! :)