为什么我在此代码中获得未捕获的异常?

时间:2013-01-20 17:45:17

标签: php exception try-catch

所以在我下面的代码中,我不确定我哪里出错了。语法使用不当,错误使用变量?请帮忙!

出于某种原因,我在浏览器中运行并返回

"Fatal error: Uncaught exception 'numexception' with message 'The numbers are not set' in C:\xampp\htdocs\php_testing\test.php:29 Stack trace: #0 {main} thrown in C:\xampp\htdocs\php_testing\test.php on line 29"

我不明白我的代码出错了?

class numexception extends Exception{}

function multiply($a,$b){
     echo $a*$b;
}


$var1 = 5;
//$var2 = 2; as you can see variable 2 is not set as I commented it out to test
//the exception

if(!isset($var1) or !isset($var2)){
  throw new numexception("The numbers are not set");
}

try{
    multiply($var1,$var2);
}
catch(numexception $e){
    echo "This exception was caught:".$e->getMessage();
}

echo "The script then continues";

1 个答案:

答案 0 :(得分:5)

throw不在try中,因此不能catch

你的代码在做什么就像用高尔夫球击打某人,然后大喊“Fore!”。