php?拋異常

來源:互聯網
上載者:User
php如何自訂異常類,捕獲異常和拋出異常.
或者給個例子參考下


回複討論(解決方案)

try {      throw new Exception($error);  } catch (Exception $e) {      echo $e->getMessage();  }

try{
throw new Exception($error);
}catch(Exception $e){
echo $e->getCode();
}

set_exception_handler('myException');test(-1);function test($a){if($a < 0){throw new Exception('error');}return $a;}function myException($e){$msg='code : '.$e->getCode().'
message : '.$e->getMessage();echo $msg;}

http://www.php.net/manual/zh/language.exceptions.php

function inverse($x) {    if (!$x) {        throw new Exception('Division by zero.');    }    return 1/$x;}try {    echo inverse(5) . "\n";    echo inverse(0) . "\n";} catch (Exception $e) {    echo 'Caught exception: ',  $e->getMessage(), "\n";}// Continue executionecho "Hello World\n";以上常式會輸出:0.2Caught exception: Division by zero.Hello World

自訂一個異常類

getMessage());        }    }}$foo = new Test;$foo->testing();?>

try {

throw new Exception($error);

} catch (Exception $e) {

echo $e->getMessage();

}

樓上都是正解

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.