Reply to Re: Handling exception question

Your name:

Reply:


Posted by Dikkie Dik on 05/31/06 18:32

> I have code like this:
>
> class MyException extends Exception {}
>
> function x() {
> try {
> throw new MyException()
> } catch (Exception $e) {
> echo 'Exception catched';
> exit;
> }
>
> try {
> x();
> } catch (MyException $e) {
> echo 'MyException catched';
> exit;
> }
>
>
>
> I cannot understand why this code catches Exception instead of
> MyException? Can you please tell me what is wrong with it?


There's nothing wrong with it. In function x you throw an exception (a
MyException to be exact) and catch it again, because you catch any
exception. So the function x does not throw an exception to the outside
world. If you want to catch specific exceptions, state them in the catch
part:

Try{...}
catch(SomeException $e){...}
catch(SomeOtherException $se){...}

So you can use more than one catch. If you want to handle all
exceptions, but some specifically, state the specific ones first, and
the more generic ones below them.

Best regards

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация