note 52612 added to language.exceptions
| From: | jd at wuputah dot com | Date: | Sat, 07 May 2005 02:15:03 +0000 |
| Subject: | note 52612 added to language.exceptions | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-89046@lists.php.net to get a copy of this message | ||
PHP5 supports exception throwing inside a function, and catching it outside that function call.
There is no mention of this in documentation but it works just fine, as tested by this sample code:
<?php
function exceptionFunction() {
throw new Exception("Throwing an exception!");
}
try {
exceptionFunction();
} catch (Exception $e) {
echo "Exception caught!\n";
}
?>
The result in PHP 5.0.3 is "Exception caught!"
Further tests show that nested functions with exceptions, methods throwing exceptions, etc all work
the same way. This is like declaring all classes (or methods) in Java as "class ClassName
throws Exception". While I consider this a good thing, you should be aware that any thrown
exception will propagate up your stack until it is either caught or runs out of stack.
----
Manual Page -- http://www.php.net/manual/en/language.exceptions.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+52612
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52612&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52612&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52612&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52612&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52612&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+52612&report=yes
Search -- http://master.php.net/manage/user-notes.php