Re: Exception handling in PHP
| From: | Paul Scott | Date: | Thu, 07 Feb 2008 06:59:12 +0000 |
| Subject: | Re: Exception handling in PHP | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-268844@lists.php.net to get a copy of this message | ||
On Wed, 2008-02-06 at 22:52 -0800, Prabath Kumarasinghe wrote:
> Hi All
>
> I'm little bit confusing with PHP exception handling.
> Could you able to explain how to put try{} and
> catch(){} in a proper way in PHP. I had already read
> php exception manual but it didn't help me to get
> exact idea about exception handling in php.
>
Well, its pretty simple really...
In your objects that you create, just put in a statement that throws an
exception of some sort (I like to extend the built in exception handler
with a custom one that deals properly with db errors as well as PHP
ones) and then try{} and catch(){} them in your business logic.
example:
class someclass {
public function foo()
{
// do something
...
else {
throw new Exception("uh-oh - we have a problem!");
}
}
}
$thing = new someclass;
try {
$thing->foo();
}
catch(Exception $e) {
echo $e->getMessage();
exit;
}
--Paul