Should the PHP community start using more descriptive exceptions?
I work a lot with Zend Framework and I just looked into Kohana and it seems strange to me that this is a typical scenario within this framework:
throw Some_Components_Exception( 'invalid argument' );
Where I believe this wouldn't be much more useful:
throw Some_Components_InvalidArgumentException( 'whatever discription' );
Because it's easier to catch.
I suspect, but immediately admit it is biased, that the old practice is common in the PHP community. Should we, the PHP community, make more use of these descriptive expectation types?
a source to share
Yes, I would recommend using the Exception subtypes provided by the SPL. It allows you to call code (either within the framework or in your application) to handle different types of exceptions in different ways.
A DomainException
may indicate invalid user input, which you have to tell the user and allow them to re-enter data. A BadMethodCallException
may indicate a flaw in your code and you must register and handle it in a different way.
But these different exception scenarios have nothing to do with the class or component that threw the exception. A BadMethodCallException
should probably be handled similarly whether it happens in an MVC component or a DB access layer.
I was the project manager for the Zend Framework through its 1.0 release. I wanted to refactor the exception hierarchy and I thought it was an arbitrary decision (before I joined the project) to use a single exception for each component. It didn't make sense.
Unfortunately, resynchronizing exceptions is not as important as getting the product up to version 1.0. I had to keep track of the scheduling priority that Zend had given for the project, and to do that, it had to postpone pretty much anything that was not absolutely necessary to get to the full version.
a source to share