Skip to content

Instantly share code, notes, and snippets.

@fabriceleal
Last active December 23, 2015 02:59
Show Gist options
  • Save fabriceleal/6570963 to your computer and use it in GitHub Desktop.
Save fabriceleal/6570963 to your computer and use it in GitHub Desktop.
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