Re: Handling exception question
- From: Dikkie Dik <nospam@xxxxxxxxxx>
- Date: Wed, 31 May 2006 20:32:10 +0200
I have code like this:
class MyException extends Exception {}
function x() {
try {
throw new MyException()
} catch (Exception $e) {
echo 'Exception catched';
exit;
}
try {
x();
} catch (MyException $e) {
echo 'MyException catched';
exit;
}
I cannot understand why this code catches Exception instead of
MyException? Can you please tell me what is wrong with it?
There's nothing wrong with it. In function x you throw an exception (a MyException to be exact) and catch it again, because you catch any exception. So the function x does not throw an exception to the outside world. If you want to catch specific exceptions, state them in the catch part:
Try{...}
catch(SomeException $e){...}
catch(SomeOtherException $se){...}
So you can use more than one catch. If you want to handle all exceptions, but some specifically, state the specific ones first, and the more generic ones below them.
Best regards
.
- References:
- Handling exception question
- From: Paul Czubilinski
- Handling exception question
- Prev by Date: HELP!! PHP Warning: PHP Startup: Unable to load dynamic library 'c:\PHP\Ext\php_mysql.dll' - The specified procedure could not be found. in Unknown on line 0
- Next by Date: Re: PHP Beginner question
- Previous by thread: Re: Handling exception question
- Next by thread: Does any one know how to use php to connect sybase 5.5?
- Index(es):