ifubad Posted August 8, 2009 Share Posted August 8, 2009 When using multiple catch for custom exceptions, as of now, I have the custom exception classes as separate include files. Like so Catch block catch(FileException $e){ $e->showError(); } catch(MailException $e){ $e->logError(); } custom Exception classes Filename: FileException.inc.php class FileException extends Exception{ public function showError(){ die($this->getMessage()); } } Filename: MailException.inc.php class MailException extends Exception{ public function logError(){ error_log($this->getMessage(),1,'sysadmin@domain.com'); } } Is it possible to keep the different custom exceptions in a single/central file? If so, can you provided a simple example on how to implement. Or, is it that when using multiple custom exceptions, they HAVE to be kept in separate files? TIA Quote Link to comment https://forums.phpfreaks.com/topic/169318-solved-custom-exceptions-using-multiple-catch/ Share on other sites More sharing options...
trq Posted August 8, 2009 Share Posted August 8, 2009 You would simply define your exception classes within the one file. eg; exceptions.php <?php class FileException extends Exception{ public function showError(){ die($this->getMessage()); } } class MailException extends Exception{ public function logError(){ error_log($this->getMessage(),1,'sysadmin@domain.com'); } } However, this makes things more difficult when using any autoloading mechanism (as you may be), in that case its best to store your exceptions within there own files. Quote Link to comment https://forums.phpfreaks.com/topic/169318-solved-custom-exceptions-using-multiple-catch/#findComment-893466 Share on other sites More sharing options...
ifubad Posted August 8, 2009 Author Share Posted August 8, 2009 You would simply define your exception classes within the one file. eg; Thank you, that part I do know. What I was wondering is that if it was possible to store different exception classes in a single "include" file. That way, I figured it MAY make it easier to maintain all of the custom exceptions. Quote Link to comment https://forums.phpfreaks.com/topic/169318-solved-custom-exceptions-using-multiple-catch/#findComment-893474 Share on other sites More sharing options...
trq Posted August 8, 2009 Share Posted August 8, 2009 What I was wondering is that if it was possible to store different exception classes in a single "include" file. As I said, yes, just store them in the same file! Quote Link to comment https://forums.phpfreaks.com/topic/169318-solved-custom-exceptions-using-multiple-catch/#findComment-893475 Share on other sites More sharing options...
ifubad Posted August 9, 2009 Author Share Posted August 9, 2009 However, this makes things more difficult when using any autoloading mechanism (as you may be), in that case its best to store your exceptions within there own files. cool, tried it and I understood your last reply. Thanks What do you mean by autoloading mechanism and how does it make it more difficult? Quote Link to comment https://forums.phpfreaks.com/topic/169318-solved-custom-exceptions-using-multiple-catch/#findComment-894286 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.