Last active
December 23, 2015 02:59
-
-
Save fabriceleal/6570963 to your computer and use it in GitHub Desktop.
My exception throwing / handling aproach (http://www.reddit.com/r/readablecode/comments/1mflfw/how_to_work_with_exceptions/)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FormFoo : Form { | |
// ... | |
// This is an UI control event handler, so any errors | |
// that happen are going to be reported to the user (via the MessageBox.Show) | |
private void Bt1_Click(object sender, EventArgs e) { | |
try { | |
DoStuff(); | |
DoStuff2(); | |
}catch(Exception ex){ | |
MessageBox.Show(....); | |
} | |
} | |
// These are not UI event handlers, so they should throw exceptions | |
// if they are unable to do their task properly, and the callee should be | |
// in charge of recovering/propagating failure | |
private void DoStuff() { | |
try { | |
// ... | |
DoStuff3(); | |
// ... | |
}catch(Exception ex) { | |
throw new Exception("Error on DoStuff()", ex); | |
} | |
} | |
private void DoStuff2() { | |
try { | |
// ... | |
}catch(Exception ex) { | |
throw new Exception("Error on DoStuff2()", ex); | |
} | |
} | |
private void DoStuff3() { | |
try { | |
// ... | |
if(something_is_not_right){ | |
throw new Exception("Something is not right"); | |
} | |
// ... | |
}catch(Exception ex) { | |
throw new Exception("Error on DoStuff3()", ex); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment