I just noticed today that the try..catch block requires the use of curly braces, e.g.:
- <?php
-
- try {
-
- throw new Exception('unox');
-
- } catch (Exception $e) echo $e->getMessage();
-
- ?>
..won't work. I wonder why..
I just noticed today that the try..catch block requires the use of curly braces, e.g.:
- <?php
-
- try {
-
- throw new Exception('unox');
-
- } catch (Exception $e) echo $e->getMessage();
-
- ?>
..won't work. I wonder why..
My name is Evert, and I've been writing semi-regularly on this blog since 2006.
I'm currently available for contract work.
Dropbox is a simple cross-platform online backup and sync application. The first 2GB of space is free, and both you and me get an extra 250MB extra space if you sign up through this link.
I don't know if this was a serious question or not, but I have the weird feeling that you can't mix the short (no { }) notation with the usage of brackets earlier in the same statement.
Well, dealing with an exception in a decent manner probably requires more than one line in most cases :).
Inconsitent due to parser inabilities.
Reminds me of $obj->foo()->bar() not working in PHP 4.x
Yes, if you start looking for consistency in the PHP parser, I think you'll be disappointed :)
I believe the same applies to C++ and Java. Not sure about their intentions, but maybe it was to enforce clean and explicit code (something that should be very important when dealing with exceptions).
Stefan, you can mix both notations on if-else blocks, so that's not really the case.
I found out its even the case with javascript.. strange
Very use full try catch example thankyou.
you must use crurly brackets for the catch as well. the following works fine for me;
[code]
// Load the XML source
$xml = new DOMDocument;
try{
global $url,$xml;
$xml->load($url);
}catch (Exception $e){
echo 'failed';
$xml->load('empty.xml');
}
[/code]
no big deal, just use braces always.
.
Yes, It's really powerful thing in PHP5 - i like it very much!
I found the same thing, I think its because the try catch functionality is only supported from PHP 5 onwards, I happened to be using PHP 4.4.9
Yea, try..catch on PHP4 will definitely not work.. My note was on some of the little inconsistencies that are in the PHP engine..
why we use try catch block and if it is what is the sntax
try {
throw new Exception('unox');
} catch (Exception $e){
echo $e->getMessage();
}
You can't use try without the brackets like if or for
getMessage();
}
?>
is working fine like this
You can use the try / catch block when you write one or many exceptions in you Class, for exemple:
\n";
} else {
throw new Exception ("Error: the variable isnt a string.
\n");
}
}
}
try{
$object = new Test();
// here will echo "Olá Felipe."
$object->hello("Felipe");
// here you will have one error.
// then the catch will send the error message.
// because the method expect receive a string not an interger
$object->hello(15);
}catch (Exception $e){
echo $e->getMessage() . "\n
";
}
?>
(sorry about my poor english)
Oh my.. still comments rolling in on this one.
Let my try to repeat my point..
I think it's weird, that try..catch requires brackets, while other control statements such as while, for, foreach & if do not require brackets.
I have no problem figuring out how exceptions work, but thank you..
Hi, I am trying the following code, please tell me what's wrong.
try
{
//Dividing 5 by Zero to trigger an exception.
$num = 5 / 0;
}
catch(Exception $e)
{
echo "Error Message: " . $e->getMessage();
}
Please tell me what is wrong with me code.
Thanks.