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)?

Tuckbros
  • 417
  • 3
  • 13
  • 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). – Dave May 29 '20 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...) – Tuckbros May 29 '20 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. – Dave May 29 '20 at 11:46
  • The thing I want to catch is the same as the one catched by the python `KeyboardInterrupt` – Tuckbros May 29 '20 at 12:25
  • Then please [edit] your code to include a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve), as explained in stackoverflow.com's [help center](https://stackoverflow.com/help/how-to-ask) so others may better assist. What you have in the question now isn't something anyone could replicate and test with. – Dave May 29 '20 at 12:59
  • I don't know how to make it more understandable than how to translate the a python `except KeyboardInterrupt: pass` statement in php. – Tuckbros May 29 '20 at 13:38
  • What is `except KeyboardInterrupt: pass` in Python? You've tagged this for PHP but assume everyone knows what that statement does. What is the code in the `try` portion of the block? – Dave May 29 '20 at 13:46
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/214912/discussion-between-dave-and-tuckbros). – Dave May 29 '20 at 13:46

0 Answers0