note 79843 deleted from language.operators.errorcontrol by bjori
| From: | bjori@php.net | Date: | Sat, 15 Dec 2007 10:11:49 +0000 |
| Subject: | note 79843 deleted from language.operators.errorcontrol by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-133537@lists.php.net to get a copy of this message | ||
Note Submitter: hyponiq at gmail dot com
----
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.