Try-Catch Syntax Weirdness

Posted by Tim Koschützki, on Jun 21, 2007 - in PHP & CakePHP » Other

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:

php
  1. try {
  2.     $error = 'Throw this error';
  3.     throw new Exception($error);
  4.  
  5.     echo 'Never get here';
  6.  
  7. } catch (Exception $e)
  8.     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:

php
  1. try {
  2.     $error = 'Throw this error';
  3.     throw new Exception($error);
  4.  
  5.     echo 'Never get here';
  6.  
  7. } catch (Exception $e) {
  8.     echo 'Exception caught: ',  $e->getMessage(), "\n";
  9. }

I always use curly braces by default and just seemed to have forgotten about them for a moment today. *shrug*

Print this Post | Digg This | Stumble It | Delicious

13 Comments

Nick 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 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 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 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 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 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 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 on Jun 25, 2007:

Hehe. =]

Hrm probably it was Smalltalk.

Hrm hrm. *shrug*

Vadim Sacharow on Jun 25, 2007:
Derik 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 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 on Sep 04, 2007:

Good idea, speps. That could indeed be a reason.

Add a comment