错误处理try catch

<?php
function inverse($x) {
if (!$x) {
throw new Exception('被除数不能为0');
}
if ($x>31) {
throw new Exception('月数不能大于31');
}
return 1/$x;
}

try
{
echo inverse(5) . "<hr>";
echo inverse(10) . "<hr>";
echo inverse(2) . "<hr>";
echo inverse(0) . "<hr>";
}
catch (Exception $e)
{
echo '错误提示: ', $e->getMessage(), "<hr>";
}

?>
posted @ 2015-11-23 16:30  九分  阅读(157)  评论(0编辑  收藏  举报