note 106168 added to language.exceptions.extending
| From: | Dor at osu1 dot php dot net | Date: | Sat, 15 Oct 2011 21:30:47 +0000 |
| Subject: | note 106168 added to language.exceptions.extending | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-183906@lists.php.net to get a copy of this message | ||
It's important to note that subclasses of the Exception class will be caught by the default
Exception handler
<?php
/**
* NewException
* Extends the Exception class so that the $message parameter is now mendatory.
*
*/
class NewException extends Exception {
//$message is now not optional, just for the extension.
public function __construct($message, $code = 0, Exception $previous = null) {
parent::__construct($message, $code, $previous);
}
}
/**
* TestException
* Tests and throws Exceptions.
*/
class TestException {
const NONE = 0;
const NORMAL = 1;
const CUSTOM = 2;
public function __construct($type = self::NONE) {
switch ($type) {
case 1:
throw new Exception('Normal Exception');
break;
case 2:
throw new NewException('Custom Exception');
break;
default:
return 0; //No exception is thrown.
}
}
}
try {
$t = new TestException(TestException::CUSTOM);
}
catch (Exception $e) {
print_r($e); //Exception Caught
}
?>
Note that if an Exception is caught once, it won't be caught again (even for a more specific
handler).
----
Server IP: 212.150.176.150
Probable Submitter: 80.179.184.165
----
Manual Page -- http://www.php.net/manual/en/language.exceptions.extending.php
Edit -- https://master.php.net/note/edit/106168
Del: integrated -- https://master.php.net/note/delete/106168/integrated
Del: useless -- https://master.php.net/note/delete/106168/useless
Del: bad code -- https://master.php.net/note/delete/106168/bad+code
Del: spam -- https://master.php.net/note/delete/106168/spam
Del: non-english -- https://master.php.net/note/delete/106168/non-english
Del: in docs -- https://master.php.net/note/delete/106168/in+docs
Del: other reasons-- https://master.php.net/note/delete/106168
Reject -- https://master.php.net/note/reject/106168
Search -- https://master.php.net/manage/user-notes.php