Re: Exception handling in PHP

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

All Email originating from UWC is covered by disclaimer 
http://www.uwc.ac.za/portal/public/portal_services/disclaimer.htm 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux