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

Web mentions

Comments

  • Stefan

    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

    Mathieu Kooiman

    Well, dealing with an exception in a decent manner probably requires more than one line in most cases :).
  • Sebastian

    Sebastian

    Inconsitent due to parser inabilities. Reminds me of $obj->foo()->bar() not working in PHP 4.x
  • Hans

    Hans

    Yes, if you start looking for consistency in the PHP parser, I think you'll be disappointed :)
  • Daniel Luz

    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

    Evert

    I found out its even the case with javascript.. strange
  • Manjunath

    Manjunath

    Very use full try catch example thankyou.
  • eddie

    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

    Jesse Couch Vancouver

    no big deal, just use braces always.
  • Actionscript 3.0

    Actionscript 3.0

    .
  • Snowcore

    Snowcore

    Yes, It's really powerful thing in PHP5 - i like it very much!
  • Jamie

    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.9
  • Evert

    Evert

    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

    soumya

    why we use try catch block and if it is what is the sntax
  • asd

    asd

    try { throw new Exception('unox'); } catch (Exception $e){ echo $e->getMessage(); }
  • tarek

    tarek

    You can't use try without the brackets like if or for <?php try{ throw new Exception('unox'); }catch (Exception $e){ echo $e->getMessage(); } ?> is working fine like this
  • tnt2br

    tnt2br

    You can use the try / catch block when you write one or many exceptions in you Class, for exemple: <?php Class Test { public function hello($name) { if (is_string($name)){ echo "Olá $name. <br />\n"; } else { throw new Exception ("Error: the variable isnt a string.<br />\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<br />"; } ?> (sorry about my poor english)
  • Evert

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