PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

Rob Allen's Blog: On Exceptions

Note: This article was originally published at PHPDeveloper on 9 September 2010.
PHPDeveloper

Rob Allen caught an interesting feature of the proposed exception handling in the Zend Framework v2 - the ability to throw an exception and catch it at the interface level, not just the class.

I've been reading the Proposal for Exceptions in ZF2 and like it. One thing that caught my attention was that it suggests that you can catch an interface. I hadn't heard of that ability before, so I pulled out my trusty text editor to have a play.

He creates a basic bit of code - an interface for the exceptions, an SPL exception class and a normal exceptions class - as well as a basic generic class that throws exceptions based on each. The result is that catching exceptions for the class types works as well as catching it on the "ExceptionInterface" level too.

We now have the ability with ZF2 to be able to use different exception classes to represent different error types rather than using string comparison and, at the same time, we can have a single catch() for when we don't need that level of granularity.