php try … else

后端 未结 6 1568
余生分开走
余生分开走 2021-01-07 18:50

Is there something similar in PHP to the try ... else in Python?

I need to know if the try block executed correctly as when the block executed correctly

6条回答
  •  半阙折子戏
    2021-01-07 19:22

    You can use try { } catch () { } and throw. See http://php.net/manual/en/language.exceptions.php

    try {
        $a = 13/0; // should throw exception
    } catch (Exception $e) {
        echo 'Caught exception: ',  $e->getMessage(), "\n";
    }
    

    or manually:

    try {
        throw new Exception("I don't want to be tried!");
    } catch (Exception $e) {
        echo 'Caught exception: ',  $e->getMessage(), "\n";
    }
    

提交回复
热议问题