I have searched for an answer to my question but not been able to find one. Apologies if the answer is there and I am duplicating!
I keep seeing try/catch code such as…..
try
{
//Do whatever
}
catch (Exception ex)
{
MessageBox.Show("Oops, something went wrong!");
}
Which will result in a warning ex is never used.
So my question is… Although ex is never used is there any benefit in the declaration? I was told that maybe it adds detail to the stack trace? Sometimes I see catch(Exception) which stops the warning but what benefits does this bring, if any? If I was to write this and not use the exception in any way I wouldn’t declare ex…
try
{
//Do whatever
}
catch
{
MessageBox.Show("Oops, something went wrong!");
}
Not a big problem but it would be good to know for sure!
Thanks
Fred
You can use the following pattern, still declaring the specific exception type, without a variable, to ensure Structured Exception Handling (SEH) is still happening:
This is not a practice I would normally use, as I would probably log the exception details if not rethrowing it.