debuggable

 
Contact Us
 

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:

try {
    $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:

try {
    $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*

 
&nsbp;

You can skip to the end and add a comment.

Nick  said on Jun 22, 2007:

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.

[...] 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 [...]

php programmers said on Jun 23, 2007:

I guess the try catch in java works similar too

http://www.exampledepot.com/egs/Java%20Language/TryCatch.html

Tim Koschuetzki said on Jun 24, 2007:

Now this seems really weird...in Java, C++ and PHP it seems to be "wrongly" implemented. I reckon a conspiracy...

Micah said on Jun 25, 2007:

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.

Vadim Sacharow  said on Jun 25, 2007:

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

Tim Koschuetzki said on Jun 25, 2007:

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. :)

Vadim Sacharow  said on Jun 25, 2007:

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 :)

Tim Koschuetzki said on Jun 25, 2007:

Hehe. =]

Hrm probably it was Smalltalk.

Hrm hrm. *shrug*

Vadim Sacharow  said on Jun 25, 2007:

Just for comparison: Exceptions ind different languages

Derik  said on Jun 25, 2007:

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

speps said on Sep 04, 2007:

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.

Tim Koschuetzki said on Sep 04, 2007:

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.