Hi All
I read a few articles about php5 exception handling, which is now very similar to java. I'm still missing information though.
1 - There is a built in class called Exception which can be extended to create custom exceptions. Are there any built in common exceptions, like say FileNotFoundException, or do we have to define ourself all our exceptions under the super class Exception.
2 - Is there any recommendation about how to organise the exception code. Is it a good idea to have all the application exceptions in one file, or an exception file per exception class.... and so on.
In Java this:
catch(Exception e) {
is a bad idea because you lose the information about what the exception is. But most of the article about php5 exception handling use it.
3 - Is any of the php functions throwing exception? My own defined methods might crash, but mysql_connect can crash too for various reason. I'd say that php function don't throw exceptions but I'd like to know more about why and how.
Any comment/advice welcome! thanks1 - Yes Exception is the only defined one (for now)
2 - I would say one exception per file (for use with __autoload()) but it's up to you.
3 - PHP functions don't throw exceptions unfortunately, but I have noticed that SimpleXMLIterator does, and so I guess most of the SPL classes do throw exceptions of some kind.Thanks for your reply. I will have to look a bit more into that _autoload thingy as I don't have a clue what it is Actually there are about a dozen presubclassed exceptions in PHP's SPL.
Exception
|
+-LogicException
| |
| +-BadFunctionCallException
| |
| +-DomainException
| |
| +-InvalidArgumentException
| |
| +-LengthException
| |
| +-OutOfRangeException
|
+-RuntimeException
|
+-OutOfBoundsException
|
+-OverflowException
|
+-RangeException
|
+-UnderflowExceptiongood one mate, I wonder how I didn't find that page before
<!-- m --><a class="postlink" href="http://www.php.net/~helly/php/ext/spl/classException.html">http://www.php.net/~helly/php/ext/spl/c ... ption.html</a><!-- m -->
thanks
I read a few articles about php5 exception handling, which is now very similar to java. I'm still missing information though.
1 - There is a built in class called Exception which can be extended to create custom exceptions. Are there any built in common exceptions, like say FileNotFoundException, or do we have to define ourself all our exceptions under the super class Exception.
2 - Is there any recommendation about how to organise the exception code. Is it a good idea to have all the application exceptions in one file, or an exception file per exception class.... and so on.
In Java this:
catch(Exception e) {
is a bad idea because you lose the information about what the exception is. But most of the article about php5 exception handling use it.
3 - Is any of the php functions throwing exception? My own defined methods might crash, but mysql_connect can crash too for various reason. I'd say that php function don't throw exceptions but I'd like to know more about why and how.
Any comment/advice welcome! thanks1 - Yes Exception is the only defined one (for now)
2 - I would say one exception per file (for use with __autoload()) but it's up to you.
3 - PHP functions don't throw exceptions unfortunately, but I have noticed that SimpleXMLIterator does, and so I guess most of the SPL classes do throw exceptions of some kind.Thanks for your reply. I will have to look a bit more into that _autoload thingy as I don't have a clue what it is Actually there are about a dozen presubclassed exceptions in PHP's SPL.
Exception
|
+-LogicException
| |
| +-BadFunctionCallException
| |
| +-DomainException
| |
| +-InvalidArgumentException
| |
| +-LengthException
| |
| +-OutOfRangeException
|
+-RuntimeException
|
+-OutOfBoundsException
|
+-OverflowException
|
+-RangeException
|
+-UnderflowExceptiongood one mate, I wonder how I didn't find that page before
<!-- m --><a class="postlink" href="http://www.php.net/~helly/php/ext/spl/classException.html">http://www.php.net/~helly/php/ext/spl/c ... ption.html</a><!-- m -->
thanks