Exceptions in PHP

Learn about exceptions in PHP and their impure behavior.

Exceptions feature prominently in PHP code and can be considered central to error handling in the language. Exceptions are, for all their illustrative cogency, however, impure. This chapter offers a description of exceptions, insights into the questionable purity of exceptions, and the possible alternatives to exceptions for cleaner error handling that are suitable for function composition.

Exceptions are impure

Exceptions are quite often used as signifiers of some kind of fault. An illegal list index here, bad API call there—exceptions are pretty much a staple of a lot of PHP error handling. The thing about exceptions, however, is that despite their ability to preempt explosive disasters, they do not work well enough to not trigger one. Exceptions, in providing vivid error descriptions, create complexities of their own. The real downside to using exceptions is that they break referential transparency. An exception does not inherently possess a type—equational reasoning is not a possibility.

Get hands-on with 1200+ tech skills courses.