Eric Diviney
Eric Diviney

Reputation: 327

Why do I get an uncaught exception in this code?

So in my code below, I'm not sure where i went wrong. Improper use of syntax, wrong use of variables? Please help!

For some reason i run this in the browser and get back

"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".

I don't understand where my code went wrong???

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";

Upvotes: 0

Views: 56

Answers (1)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324640

The throw is not in the try, so it can't be catched.

What your code is doing is like hitting someone with a golf ball, and then yelling "Fore!".

Upvotes: 5

Related Questions