Which is the better design approach - to return bool or to throw an exception?
-
I'm wondering which approach is better. Let's say we have a method that, for example, sends a notification email. void SendNotificaitonEmail(); So, I can edit my SendNotificaitonEmail() method so it now does the following: bool SendNotificaitonEmail(out string errorMessage) { try { // This is the code that handles the actual sending of the email. // .. } catch(Exception ex) { errorMessage = ex.Message; return false; } } But isn't this wrong, in terms of design? For example, the errorMessage variable has nothing to do with the concept of SendNotificationEmail() method. Moreover, I should then add two new variables to all of my methods - a boolean one, stating the result of the method (true/false), and a string one, containing the error message, if one occurs. The other way is to create my custom exceptions and handle them in the other methods that are invoking the first one. public void SendNotificaitonEmail() { try { // This is the code that handles the actual sending of the email. // .. if (somethingIsWrong == true) { throw new MyCustomException(); } } catch(Exception ex) { // Other exception handling code. // .. } } public void OtherMethod() { try { SendNotificaitonEmail(); } catch(MyCustomException ex) { // Exception handling code. // .. } } Edit: Let's say I want to be sure that all of the operations, handled in the DAL code execute successfully. I have methods like UpdateUserData, GetUserById, ChangeUserPicture. So, if I want to check whether these operations have executed successfully, I should add some additional variables like: bool UpdateUserData(User userToUpdate, out string errorMessage); User GetUserById(int id, out bool isError, out string errorMessage); bool ChangeUserPicture(Picture picture, int id, out string errorMessage); // .. I have a simple app that uses all these methods: stringerrorMessage; boolisUserUpdatedSuccessfully = UpdateUserData(someUserToUpdate, outerrorMessage); if (isUserUpdatedSuccessfully == true) { // If the DAL operation was executed successfully, do something.. } else { // Code that informs the user that an error has occurred. MyCustomErrorLogger(errorMessage); }
-
Answer:
Through an exception. You can through multiple exception for different errors within one function. Making the error details more informative, which can be used while debugging. It helps you keep the logs as well.
Siddharth Gupta at Quora Visit the source
Related Q & A:
- Which is the better place to do an MSc in theoretical physics, Imperial College London or Perimeter Institute?Best solution by Quora
- Which bike is better tvs apache or new cbz?Best solution by Yahoo! Answers
- Which is the best design mag out there?Best solution by howdesign.com
- Which country is better to study fashion design?Best solution by Yahoo! Answers
- Which monitor is better for graphic design?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.