|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50699 Cannot throw Exceptions in __toString()
Submitted: 2010-01-08 22:22 UTC Modified: 2012-12-05 01:19 UTC
From: gmblar+php at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.3.1 OS: *
Private report: No CVE-ID: None
 [2010-01-08 22:22 UTC] gmblar+php at gmail dot com
Cannot throw Exceptions in __toString(). Instead it produces a Fatal 

Reproduce code:

class bar {

    public function __toString() {
        throw new Exception('Incomplete Data');


$foo = new bar();
echo $foo;


Expected result:
Fatal error: Uncaught exception 'Exception' with message 'Incomplete 
Data' in /-:6

Actual result:
Fatal error: Method bar::__toString() must not throw an exception in /- 
on line 0


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-01-08 22:24 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

__toString() must not throw exceptions
 [2010-01-08 22:51 UTC] gmblar+php at gmail dot com
Current behavior of __toString is crap. Why i can use trigger_error() 
bug not throw Exception() in __toString()? Please fix it.
 [2012-12-04 13:05 UTC] benjamin dot morel at strictcoding dot co dot uk
@bjori The documentation does not say that __toString() must not throw an 
exception. The fact is, this is currently an undocumented behaviour, that could 
therefore be changed without causing much pain (we're supposed to write code 
based on the documented behaviour of a function, not its side effects):

A feature request has been open, I would be grateful if you could give your 
opinion there, especially now than PHP 5.5 is being developed:

Thank you.
 [2012-12-05 01:18 UTC]
Automatic comment from SVN on behalf of aharvey
Log: Add a warning about the fatal error generated when you throw from __toString().

Prompted by bug #50699, although it doesn't really fix it as such.
 [2012-12-05 01:19 UTC]
I have updated the manual to note the issue, so consider it documented. :)
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Mon Sep 26 06:05:51 2022 UTC