note 111303 added to spl.exceptions
| From: | mattsch at gmail dot com | Date: | Mon, 04 Feb 2013 16:44:17 +0000 |
| Subject: | note 111303 added to spl.exceptions | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-193523@lists.php.net to get a copy of this message | ||
These exceptions are only provided for documentation purposes. You should not throw them in your
code. Instead you should create a base exception that extends the main php Exception class. All
exceptions should extend from that base exception. The reason for this is your code will need to be
executable under a test environment. The test environment (e.g. PHPUnit) can throw exceptions
itself that it needs to be able to catch to tell you when something has failed. If you are throwing
and catching the main php exception in your code or SPL exceptions, this will force you to check if
the exception type is a phpunit exception when you are catching it. This is of course an
unacceptable situation to have test related code in production code.
Don't do this:
<?php
try {
throw new RuntimeException('error message');
catch (RuntimeException $e) {
if ($e instanceof PHPUnit_Framework_Exception) {
throw $e;
}
echo $e->getMessage();
}
?>
Do this:
<?php
class MyRuntimeException extends Exception {
}
try {
throw new MyRuntimeException('error message');
catch (MyRuntimeException $e) {
echo $e->getMessage();
}
?>
----
Server IP: 69.147.83.199
Probable Submitter: 173.7.186.88
----
Manual Page -- http://php.net/manual/en/spl.exceptions.php
Edit -- https://master.php.net/note/edit/111303
Del: integrated -- https://master.php.net/note/delete/111303/integrated
Del: useless -- https://master.php.net/note/delete/111303/useless
Del: bad code -- https://master.php.net/note/delete/111303/bad+code
Del: spam -- https://master.php.net/note/delete/111303/spam
Del: non-english -- https://master.php.net/note/delete/111303/non-english
Del: in docs -- https://master.php.net/note/delete/111303/in+docs
Del: other reasons-- https://master.php.net/note/delete/111303
Reject -- https://master.php.net/note/reject/111303
Search -- https://master.php.net/manage/user-notes.php