Try-Catch Syntax Weirdness
Posted on 21/6/07 by Tim Koschützki
Deprecated post
The authors of this post have marked it as deprecated. This means the information displayed is most likely outdated, inaccurate, boring or a combination of all three.
Policy: We never delete deprecated posts, but they are not listed in our categories or show up in the search anymore.
Comments: You can continue to leave comments on this post, but please consult Google or our search first if you want to get an answer ; ).
I just noticed today, that PHP's try catch blocks require curly braces. So the following will output a parse error:
$error = 'Throw this error';
throw new Exception($error);
echo 'Never get here';
} catch (Exception $e)
echo 'Exception caught: ', $e->getMessage(), "\n";
Anybody has an idea why it is like that? I have used curly braces by default up until now, so I just stumbled upon this weirdness today. This here works perfectly:
$error = 'Throw this error';
throw new Exception($error);
echo 'Never get here';
} catch (Exception $e) {
echo 'Exception caught: ', $e->getMessage(), "\n";
}
I always use curly braces by default and just seemed to have forgotten about them for a moment today. *shrug*
You can skip to the end and add a comment.
[...] working with his code recently, Tim Koschuetzki noticed something odd with a block of try/catch code: I just noticed today, that PHP’s try catch blocks require [...]
I guess the try catch in java works similar too
http://www.exampledepot.com/egs/Java%20Language/TryCatch.html
Now this seems really weird...in Java, C++ and PHP it seems to be "wrongly" implemented. I reckon a conspiracy...
I first noticed this in Java. I guess it's the same in PHP and C++ as well. You would think that you could go without the braces if you only had one line of code in the block.
I think, Its something how parser works.
Catch-block for the parser is something like a virtual function for the try-block. That's why catch-block must be designed like a function.
May be its so....
Hrm that sounds like a possible explanation, Vadim. Good idea!
It's just so weird that it's in most (all?) common languages like that. They all seem to have a very common implementation for try/catch. :)
I don't know, which language was first to use try/catch, but maybe all other languages have just copied the syntax to make it easier for the programmers to learn a new language.
I think, explanation for this weirdness is simple like that :)
Just for comparison: Exceptions ind different languages
Simple anwser should be that { & } SHOULD always be there. i think it is crap when developers get lazy and leave the off.
Bad developer, bad
Maybe it's to avoid the common mistake when not using the curly braces like :
if(something == 2)
doSomething();
doTheNextThing();
because the catch code is generally more sensible than an if code.
Good idea, speps. That could indeed be a reason.
This post is too old. We do not allow comments here anymore in order to fight spam. If you have real feedback or questions for the post, please contact us.
I noticed this too, while it didn't bother me since I stick to using curly braces with all conditionals and the like, it does some odd that it's required.