开发者

unhandled errors in php

How can I know during runtime that my code threw a Warning?

example

try {
    e开发者_StackOverflow中文版cho (25/0);
} catch (exception $exc) {
    echo "exception catched";
}

throws a "Warning: Division by zero" error that i can not handle on my code.


You're looking for the function set_error_handler(). Check out the sample code in the manual.

Make sure that you don't only suppress the error warnings, but instead silently redirect them to a log file or something similar. (This helps you track down bugs)


You need to handle the exception yourself as follows.e.g

function inverse($x)
{
    if(!$x)
    {
         throw new Exception('Division by zero.');
    }
    else
    { 
        return 1/$x;
    }
}


try
{
     echo inverse(5);
     echo inverse(0);
}
catch (Exception $e)
{ 
    echo $e->getMessage();
}


You need to install an error handler that converts old style php "errors" into exceptions. See an example here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜