English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories


ini_set ('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE);

try
{
dothis();
}
catch(Exception $e)
{
echo $e->getMessage();
exit();
}

function dothis()
{
throw new exception("Not cool!");
}

?>

This block of code generates the error message:

"Fatal error: Uncaught exception 'Exception' with message 'Not cool!' in /home/goddess/public_html/except.php:18 Stack trace: #0 /home/goddess/public_html/except.php(8): dothis() #1 {main} thrown in /home/goddess/public_html/except.php on line 18"\

Why?? I clearly have a catch block set up...

2007-03-07 01:52:05 · 3 answers · asked by Anonymous in Computers & Internet Programming & Design

I'm wondering if it's a problem with my code, or a problem with my server....

help!!!

2007-03-07 01:52:55 · update #1

3 answers

It is some problem with your server. This same code runs perfectly fine with me having XAMPP (PHP 5.2.0) on windows XP.

2007-03-07 19:43:15 · answer #1 · answered by Atif Majid 3 · 0 0

It generates that error because that's what you've told it to do, but in PHP, try-catch has local scope only, so you can't run try-catch the way you are trying to do it.

Because you have segmented the code that creates the exception as a function, the try block doesn't work the way you want, which is to throw the exception in a manner that your block will catch it.

Had you used a class, the behavior you expect would occur.

Read more at the PHP exceptions MAN page:

http://us2.php.net/throw

And once again, I ask that if someone doesn't know what he is talking about, he not bother answering the question.

UPDATE: It's technically a problem with your code, but it's just the way PHP works. If you used a class instead, you would be able to do what you want.

2007-03-07 10:52:21 · answer #2 · answered by Anonymous · 0 0

I don't know PHP but I would start by looking at the exception you are throwing and the Exception you are trying to catch.

It could well be a case sensitive thing!!

2007-03-07 10:21:35 · answer #3 · answered by AnalProgrammer 7 · 0 1

fedest.com, questions and answers