How do I throw an exception when I receive a signal?

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

Was this solution helpful to you?

Just Added Q & A:

Find solution

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.