In order to streamline my try
blocks, I've made a static function to handle error catching because I was already catching errors the same way in all my methods anyway.
static class Exceptional
{
public static Exception Try(Action action, [CallerMemberName] string cmn = "")
{
try
{
action();
return null;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, cmn + ": " + ex.GetType().Name);
return ex;
}
}
}
And then I've replaced all cases of
try
{
...
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace);
}
with
Exceptional.Try(() =>
{
...
});
This not only streamlines wrapping my code in try
blocks but also gives me a centralized place to globally change the way I'm handling exceptions.
I know it's not good practice to catch all exceptions and treat them all the same. I understand I'm only supposed to catch the types of exceptions I'm expecting to be thrown, but right now I'm just testing things and I don't know what's going to be thrown so I've been using try
blocks to make sure I don't get silent or unexplained failures. And that's not what my question is about.
I'd like to know if I'm in danger of changing my code's behavior by doing this. Obviously the call stack is being changed by inserting an additional function into it, but can this extra function potentially cause unexpected issues? Critiques are welcome! Thank you.
EDIT: I should mention that I'm currently wrapping entire method bodies in my Exceptional.Try
calls, so I'm particularly interested in knowing if I might encounter strange behavior in that case rather than if something outside the lambda expression might cause strange behavior.