Frage

I know similar things have been asked before but it doesn't remove my doubts on the matter.

Around all my pages Page_Load methodes and every other methode that calls a methode that connects to the database, I put my code inside a try catch statement:

try {
    //Some code

}
//When something goes wrong, catch the exception and translate it into understandable language for the end user.
catch (LaboratoryException ex) {
    switch (ex.Code) {
        case LaboratoryExceptionCode.GROUP_NOTFOUND: ErrorMessage.Text += "<p class='error'>You didn't enter the correct parameters."; break;
        default: ErrorMessage.Text += "<p class='error'>An unknown error occurred"; break;
    }
    switch (ex.Fault) {
        case LaboratoryExceptionFault.FAULT_CONSUMER: ErrorMessage.Text += " - We think you should check your entered parameters.</p>"; break;
        case LaboratoryExceptionFault.FAULT_SERVER: ErrorMessage.Text += " - Oops, our bad.</p>"; break;
        default: break;
    }
}

Is it wise to use a custom exception for every type of error?

  • If a group is not found in my database (like in the above code.);
  • If a group exists;
  • When a group cannot be updated;
  • When a group has reached the max amount of members (5);
  • etc.;

Or is it best to limit this way of error handling? I know why and when, but I don't know when too much is too much or where you have to draw the line.

A follow up question, I have a limitation to the amount of people that can be in a group, this limitation, when do I enforce this in my code? In the code behind class of the page? Or when I make a connection to the database? Or both?

War es hilfreich?

Lösung

After a few months I've become wiser and after looking up this subject again I've deducted that the following answer is the one I agree with:

https://stackoverflow.com/a/3443605/1055690

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top