PHP try..catch syntax weirdness
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..
Comments
Stefan •
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.Mathieu Kooiman •
Well, dealing with an exception in a decent manner probably requires more than one line in most cases :).Sebastian •
Inconsitent due to parser inabilities.Reminds me of $obj->foo()->bar() not working in PHP 4.x
Hans •
Yes, if you start looking for consistency in the PHP parser, I think you'll be disappointed :)Daniel Luz •
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.
Evert •
I found out its even the case with javascript.. strangeManjunath •
Very use full try catch example thankyou.eddie •
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]
Jesse Couch Vancouver •
no big deal, just use braces always.Actionscript 3.0 •
.Snowcore •
Yes, It's really powerful thing in PHP5 - i like it very much!Jamie •
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.9Evert •
Yea, try..catch on PHP4 will definitely not work.. My note was on some of the little inconsistencies that are in the PHP engine..soumya •
why we use try catch block and if it is what is the sntaxasd •
try {
throw new Exception('unox');
} catch (Exception $e){
echo $e->getMessage();
}
tarek •
You can't use try without the brackets like if or fortry{
throw new Exception('unox');
}catch (Exception $e){
echo $e->getMessage();
}
?>
is working fine like this
tnt2br •
You can use the try / catch block when you write one or many exceptions in you Class, for exemple:Class Test
{
public function hello($name)
{
if (is_string($name)){
echo "Olá $name.
\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)
Evert •
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..