簡體   English   中英

警告而不是PHP異常?

[英]warning instead of php exception?

我有這段代碼:


     try 
    {
        foreach($obj_c->getGalleries($db_conn1) as $gallery)
                {
            $gallery->Save($db_conn1);
        }
        $k = 0;
        $testing_the_exception = 15/$k; 
                //settin status to 1...
        $obj_c->set_exec_status(3, 1, $db_conn1);
    }
    catch (Exception $e) 
    {
        //settin status to 3...
    $obj_c->set_exec_status(3, 3, $db_conn1);
    echo 'Caught exception: ',  $e->getMessage(), "\n";
    }   
    unset($obj_c);

事實是,由於被零除例外,它應該進入捕獲部分,而只是彈出警告並繼續將狀態設置為1。這是預期的行為嗎? 非常感謝。

這更多是批發解決方案。 設置錯誤報告以引發異常:

<?php
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
    throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
set_error_handler("exception_error_handler");

/* Trigger exception */
strpos();
?>  

這是來自php.net上的set_error_handler文檔的示例

多數民眾贊成因為除以0是在php中的警告,而不是一個例外。 捕獲此錯誤的最佳方法是對其進行測試並拋出自己的異常。

if($k == 0)
    throw new Exception("Division by zero");
$testing_the_exception = 15/$k; 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM