# RFC: Errors as Exceptions

- Status: Inactive
- Authors: Ben Scholzen mail@dasprids.de
- Date: 2009-07-24
- Version: 1.0
- Original: https://wiki.php.net/rfc/errors_as_exceptions
- Page: https://externals.io/rfc/errors_as_exceptions
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

## Discussions on the mailing list

- [RFC: Replacing errors with Exceptions](https://externals.io/message/45070.md) — started on 2009-07-24, 31 messages
- [Error handling brainstorming](https://externals.io/message/62019.md) — started on 2012-08-03, 30 messages

---

This RFC will discuss the usage of exceptions instead of the usual, not catchable, errors.

## Introduction

Exceptions are a proper way of handling errors and are used in many other languages exclusivley.

### Why do we need exceptions instead of errors?

Currently it is not easy to work around **expected** errors comming from PHP. This does not include errors like E\_NOTICE or E\_DEPRECATED, but mainly E\_WARNING. We frequently notice this problem in the Zend Framework, where it either is bad to performance to use the shut-up-operator (@) and/or there is just no other way instead of supressing an error.

## Common Misconceptions

When, for instance, loading an XML file with simplexml\_load\_file(), it will throw an E\_WARNING when the XML is not valid. When the XML is comming from an external source, you can never be sure beforehand if it will be valid or not, so the only thing you can currently do is supressing the error and checking for the return-value “false”. This error supression not only costs time, but also avoids to check for the type of the error (may it be an invalid XML itself, the XML not conforming to the defined DTD or something else). Another example would be fsockopen(), which, when it times out, not only returns false, but also throws an E\_WARNING again.

In all those cases it would be a prefered way to be able to simply catch the error as an exception.

## Proposal and Patch

All E\_WARNINGS should be replaced by exceptions (preferably, ErrorException). An alternative to keep BC would be to allow the try/catch block to also catch errors (either only E\_WARNINGS or also other error levels). In that case, error\_reporting() set to 0 should not stop the try/catch block to catch that error.

### More about Exceptions

[http://de3.php.net/error\_exception](http://de3.php.net/error_exception)

## Changelog
