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

Eri*_*ney 0 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";
Run Code Online (Sandbox Code Playgroud)

Nie*_*sol 5

在throw不在try,所以它不能被catch编.

你的代码正在做的就像用高尔夫球击打某人,然后大喊"Fore!".