Re: [PHP] Exception handling in PHP
On Wed, 2008-02-06 at 23:37 -0800, Prabath Kumarasinghe wrote: Does that mean for every exception do we have to write our custom exception and describe it from our own message No, it means that when you want to throw a meaningful exception, you need to type in a message. I mentioned custom exceptions, because that is what I do, as I multilingualise my messages and do other things with the exception, like display an XHTML page to the users. --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
Re: [PHP] Exception handling in PHP
Understood, Thanks Paul Cheers Prabath --- Paul Scott [EMAIL PROTECTED] wrote: On Wed, 2008-02-06 at 23:37 -0800, Prabath Kumarasinghe wrote: Does that mean for every exception do we have to write our custom exception and describe it from our own message No, it means that when you want to throw a meaningful exception, you need to type in a message. I mentioned custom exceptions, because that is what I do, as I multilingualise my messages and do other things with the exception, like display an XHTML page to the users. --Paul All Email originating from UWC is covered by disclaimer http://www.uwc.ac.za/portal/public/portal_services/disclaimer.htm Looking for last minute shopping deals? Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsearch/category.php?category=shopping -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Exception handling in PHP
On Thu, 2008-02-07 at 00:20 -0800, Prabath Kumarasinghe wrote: Understood, Thanks Paul Pleasure, but please don't top post, it makes it really hard to follow a thread easily. Most people on this list take time out from their really busy day jobs to help out, and the more time that everyone can save them, the better. --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
Re: [PHP] Exception handling in PHP
Does that mean for every exception do we have to write our custom exception and describe it from our own message If you mean Do I have to write custom exception classes?, the no. You could just use the PHP Exception class. Eg. class DBException extends Exception {} try { $connection = mysql_connect(...); if (!$connection) { throw new DBException('Failed to connect to database'); } // Database exception handling code } catch (DBException $e) { // ... // Generic Exception handling code (The Exception c } catch Exception $e { // ... } That's from memory, so there may be a few errors. -- Richard Heyes http://www.websupportsolutions.co.uk Knowledge Base and Helpdesk software for £299 hosted for you - no installation, no maintenance, new features automatic and free ** New Helpdesk demo now available ** -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Exception handling in PHP
// ... That's from memory, so there may be a few errors. Seems there was. Try this: class DBException extends Exception {} try { $connection = mysql_connect(...); if (!$connection) { throw new DBException('Failed to connect to database'); } // Database exception handling code } catch (DBException $e) { // ... // Generic Exception handling code (The Exception class is // built in to PHP) } catch (Exception $e) { // ... } -- Richard Heyes http://www.websupportsolutions.co.uk Knowledge Base and Helpdesk software for £299 hosted for you - no installation, no maintenance, new features automatic and free ** New Helpdesk demo now available ** -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Exception handling in PHP
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
Re: [PHP] Exception handling in PHP
Hi Paul Is this following code work in PHP if mysql_connect fails. try{ mysql_connect('localhost','user',''); }catch(Exception $e){ echo $e-getMessage(); } Cheers Prabath --- Paul Scott [EMAIL PROTECTED] wrote: 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 Looking for last minute shopping deals? Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsearch/category.php?category=shopping -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Exception handling in PHP
On Wed, 2008-02-06 at 23:10 -0800, Prabath Kumarasinghe wrote: Is this following code work in PHP if mysql_connect fails. try{ mysql_connect('localhost','user',''); }catch(Exception $e){ echo $e-getMessage(); Probably not. I don't think that the mysql functions throw exceptions on failure. You will have to write a wrapper function to do the exception on failure, so something like function dbConnect() { $conn = mysql_connect('','',''); if(!$conn) { throw new Exception(Couldn't connect); } else { return $conn; } } 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
Re: [PHP] Exception handling in PHP
Does that mean for every exception do we have to write our custom exception and describe it from our own message Cheers Prabath --- Paul Scott [EMAIL PROTECTED] wrote: On Wed, 2008-02-06 at 23:10 -0800, Prabath Kumarasinghe wrote: Is this following code work in PHP if mysql_connect fails. try{ mysql_connect('localhost','user',''); }catch(Exception $e){ echo $e-getMessage(); Probably not. I don't think that the mysql functions throw exceptions on failure. You will have to write a wrapper function to do the exception on failure, so something like function dbConnect() { $conn = mysql_connect('','',''); if(!$conn) { throw new Exception(Couldn't connect); } else { return $conn; } } All Email originating from UWC is covered by disclaimer http://www.uwc.ac.za/portal/public/portal_services/disclaimer.htm Never miss a thing. Make Yahoo your home page. http://www.yahoo.com/r/hs -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Exception Handling in php
I think that would be nice to have a try-catch to better control of the code. Ovidiu -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] Sent: Tuesday, February 27, 2001 9:09 PM To: kevin1; [EMAIL PROTECTED] Subject: Re: [PHP] Exception Handling in php Addressed to: kevin1 [EMAIL PROTECTED] [EMAIL PROTECTED] ** Reply to note from kevin1 [EMAIL PROTECTED] Tue, 27 Feb 2001 08:10:05 -0500 Is there any equivalent to Java's try-catch or Perl's eval{BLOCK}if($@) structure in PHP? No. There has been some discussion about adding it on the developers list, but I don't expect to see it any time soon. Rick Widmer Internet Marketing Specialists http://www.developersdesk.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Exception Handling in php
Addressed to: kevin1 [EMAIL PROTECTED] [EMAIL PROTECTED] ** Reply to note from kevin1 [EMAIL PROTECTED] Tue, 27 Feb 2001 08:10:05 -0500 Is there any equivalent to Java's try-catch or Perl's eval{BLOCK}if($@) structure in PHP? No. There has been some discussion about adding it on the developers list, but I don't expect to see it any time soon. Rick Widmer Internet Marketing Specialists http://www.developersdesk.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]