<?php
//create function with an exception
function checkNum($number)
{
if($number>1)
{
throw new Exception("Value must be 1 or below");
}
return true;
}
//trigger exception
checkNum(2);
?>
上面的代码会获得类似这样的一个错误:
PHP Fatal error: Uncaught exception 'Exception' with message 'Value must be 1 or below' in /home/wangkongming/babytree/test/php/php_ob/3.php:7
Stack trace:
#0 /home/wangkongming/babytree/test/php/php_ob/3.php(12): checkNum(2)
#1 {main}
thrown in /home/wangkongming/babytree/test/php/php_ob/3.php on line 7
<?php
//create function with an exception
function checkNum($number)
{
if($number>1)
{
throw new Exception("Value must be 1 or below");
}
return true;
}
//trigger exception
try{
checkNum(2);
echo "If you see this ,the number is 1 or below";
}catch(Exception $e) {
echo 'Message: '.$e -> getMessage();
}
?>