note 79843 added to language.operators.errorcontrol

From: Date: Fri, 14 Dec 2007 21:55:32 +0000
Subject: note 79843 added to language.operators.errorcontrol
Groups: php.notes 
Request: Send a blank email to php-notes+get-133484@lists.php.net to get a copy of this message
I'd like to point out that this only works on errors and NOT exceptions (at least in OOP context that I've tried). Consider the following code: <?php class GenericClass { public $var1 = 0; public function __construct($var1NewVal) { $this->var1 = $var1NewVal; } public function surpressMyError() { if ($this->var1 > 1) { return $this->var1; } else { trigger_error('GenericClass::$var1 was not greater than 1.'); } } public function surpressMyException() { if ($this->var1 > 1) { return $this->var1; } else { throw new Exception('GenericClass::$var1 was not greater than 1.'); } } } // instanciate class $gc = new GenericClass(1); // try to surpress the error $example1 = @($gc->surpressMyError()); // does work; nothing will be output by PHP // try to surpress the exception $example2 = @($gc->surpressMyException()); // doesn't work; PHP will output a generic Expression message. ?> If you run a test on this script, you'll see that while the error was surpressed, the exception thrown was not. This is a rather peculiar behaviour, although I can understand the necessity for this as developers obviously have a very good reason for the inclusion of exceptions in their logic definitions. Whatever the case, beforewarned when trying to surpress exceptions in your PHP scripts. If you want to include surpressible errors (which may not be beneficial to your application during development), use trigger_error() instead. Otherwise, I highly recommend using Exceptions instead as they are more easily traced. ---- Server IP: 208.69.120.35 Probable Submitter: 24.236.102.19 ---- Manual Page -- http://www.php.net/manual/en/language.operators.errorcontrol.php Edit -- https://master.php.net/note/edit/79843 Del: integrated -- https://master.php.net/note/delete/79843/integrated Del: useless -- https://master.php.net/note/delete/79843/useless Del: bad code -- https://master.php.net/note/delete/79843/bad+code Del: spam -- https://master.php.net/note/delete/79843/spam Del: non-english -- https://master.php.net/note/delete/79843/non-english Del: in docs -- https://master.php.net/note/delete/79843/in+docs Del: other reasons-- https://master.php.net/note/delete/79843 Reject -- https://master.php.net/note/reject/79843 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#133484) next »