Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:54293 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 67907 invoked from network); 2 Aug 2011 16:00:22 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 2 Aug 2011 16:00:22 -0000 Authentication-Results: pb1.pair.com smtp.mail=tyra3l@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=tyra3l@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.160.170 as permitted sender) X-PHP-List-Original-Sender: tyra3l@gmail.com X-Host-Fingerprint: 209.85.160.170 mail-gy0-f170.google.com Received: from [209.85.160.170] ([209.85.160.170:46161] helo=mail-gy0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 5E/E0-63222-F0F183E4 for ; Tue, 02 Aug 2011 12:00:22 -0400 Received: by gyb13 with SMTP id 13so4646179gyb.29 for ; Tue, 02 Aug 2011 09:00:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=fMvlS9gotABIuRZcrwihyHM36hqpttRPf0On0TNOx/U=; b=bOOWc9pmeD0GcaCqa6O1k424sDt3IfCDSaIUmXeo3Elyuk5sBN24SCGaaPaLC19O0h gyyjNdXx7E1xdDF3pEtTSHHoZF0nDJNWhAD9pGwfqv2QCEClzBjpJw/y9B0YOyIICoOQ 4pOUqvUFFiNqenDWSLwmmdtYoNFHRjXSQowt0= MIME-Version: 1.0 Received: by 10.236.184.169 with SMTP id s29mr4593970yhm.68.1312300801552; Tue, 02 Aug 2011 09:00:01 -0700 (PDT) Received: by 10.147.82.17 with HTTP; Tue, 2 Aug 2011 09:00:01 -0700 (PDT) In-Reply-To: References: Date: Tue, 2 Aug 2011 18:00:01 +0200 Message-ID: To: RQuadling@gmail.com Cc: PHP internals Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] asString() method vs __toString() magic method. From: tyra3l@gmail.com (Ferenc Kovacs) On Tue, Aug 2, 2011 at 4:47 PM, Richard Quadling wrot= e: > Hi. > > Given the relatively simple code below, why can't I throw an exception > in __toString()? > > class AlwaysFails { > =C2=A0public function asString() { > =C2=A0 =C2=A0throw new Exception('Failed in asString()'); > =C2=A0} > > =C2=A0public function __toString() { > =C2=A0 =C2=A0throw new Exception('Failed in __toString()'); > =C2=A0} > } > > $failure =3D new AlwaysFails; > try { > =C2=A0echo $failure->asString(); > } > catch(Exception $ex) { > =C2=A0echo $ex->getMessage(); > } > > try { > =C2=A0echo $failure; > } > catch(Exception $ex) { > =C2=A0echo $ex->getMessage(); > } > ?> > > outputs ... > > Failed in asString() > Fatal error: Method AlwaysFails::__toString() must not throw an > exception in Z:\fa1.php on line 21 > > > > > -- > Richard Quadling > Twitter : EE : Zend : PHPDoc > @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY : bit.ly/lFnVea > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > http://stackoverflow.com/questions/2429642/why-its-impossible-to-throw-exce= ption-from-tostring --=20 Ferenc Kov=C3=A1cs @Tyr43l - http://tyrael.hu