phant0m Posted May 20, 2010 Share Posted May 20, 2010 Hi I was wondering how you use custom exceptions. I find it difficult to balance between just using different error messages and using new Exception Classes, because I want to be able to react to different kinds of Exceptions and I am not too fond of using error codes. I tend to create new Exception classes so I can just have multiple catch blocks. How do you use Exceptions? Quote Link to comment https://forums.phpfreaks.com/topic/202372-custom-exceptions-when-to-use/ Share on other sites More sharing options...
Andy-H Posted May 20, 2010 Share Posted May 20, 2010 Whats wrong with multiple catch blocks? try { //call classes/autoload or whatever you wish to do //program runs here } catch (UserErrorException $e) { //Display error } catch (FormHandlerException $e) { //Display error with form data } catch (Exception $e) { //Redirect to error page... } I can't see a problem with that? Quote Link to comment https://forums.phpfreaks.com/topic/202372-custom-exceptions-when-to-use/#findComment-1061091 Share on other sites More sharing options...
phant0m Posted May 20, 2010 Author Share Posted May 20, 2010 I was just wondering what ways there are, to respond to different kinds of errors instead of having an Exception class for each error type. Quote Link to comment https://forums.phpfreaks.com/topic/202372-custom-exceptions-when-to-use/#findComment-1061121 Share on other sites More sharing options...
phant0m Posted June 11, 2010 Author Share Posted June 11, 2010 anyone? Quote Link to comment https://forums.phpfreaks.com/topic/202372-custom-exceptions-when-to-use/#findComment-1070797 Share on other sites More sharing options...
ChemicalBliss Posted July 3, 2010 Share Posted July 3, 2010 I would use Custom Exceptions to report specific errors such as database errors or general function failure like bad input etc. You can also customize the Exception class to take separate arguments. -cb- Quote Link to comment https://forums.phpfreaks.com/topic/202372-custom-exceptions-when-to-use/#findComment-1080542 Share on other sites More sharing options...
Mchl Posted July 3, 2010 Share Posted July 3, 2010 Here's a series of blog posts that cover this topic pretty well: http://www.brandonsavage.net/exceptional-php-introduction-to-exceptions/ http://www.brandonsavage.net/exceptional-php-extending-the-base-exception-class/ http://www.brandonsavage.net/exceptional-php-nesting-exceptions-in-php/ Quote Link to comment https://forums.phpfreaks.com/topic/202372-custom-exceptions-when-to-use/#findComment-1080610 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.