0

I am trying to rewrite a python script in php. The python script is handling the KeyboardInterrupt exception this way :

except KeyboardInterrupt: pass

I was starting with this, but can't figure out what to put for ???

} catch (Exception $e) {
    if ($e->getCode() == ??? ) {
        //I have nothing to do but I could
    }
}

I hoped to find something in the doc in the runtime exceptions https://www.php.net/manual/en/class.runtimeexception.php but I found nothing relevant. I also found this for php 7.4+ on windows (but is involving a quite new version of php and restricted on windows) PHP CLI in Windows: Handling Ctrl-C commands?

Is there a cross-platform way to catch this exception in php ? and if yes, how ? Is there a documented list of php builtin exceptions (code and message)?

8
  • If you do a print_r or var_dump of $e you should see what the code is. The codes that may be returned depend on where the exception is being thrown from (PDO etc). Commented May 29, 2020 at 11:41
  • the whole code is embedded in the try block, it can happen anywhere. Maybe ̀exception` is not the right approach and signals or events should be handled instead (if better...) Commented May 29, 2020 at 11:45
  • Then it isn't clear what you are trying to catch. Normally a try/catch block is used around a very specific "thing" that you want to make sure either works or doesn't. Commented May 29, 2020 at 11:46
  • The thing I want to catch is the same as the one catched by the python KeyboardInterrupt Commented May 29, 2020 at 12:25
  • Then please edit your code to include a Minimal, Complete, and Verifiable example, as explained in stackoverflow.com's help center so others may better assist. What you have in the question now isn't something anyone could replicate and test with. Commented May 29, 2020 at 12:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.