php try … else

后端 未结 6 1512
余生分开走
余生分开走 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:26

    There is try-catch in php.

    Example:

    function inverse($x) {
        if (!$x) {
            throw new Exception('Division by zero.');
        }
        else return 1/$x;
    }
    
    try {
        echo inverse(5) . "\n";
        echo inverse(0) . "\n";
    } catch (Exception $e) {
        echo 'Caught exception: ',  $e->getMessage(), "\n";
    }
    
    // Continue execution
    echo 'Hello World';
    

提交回复
热议问题