C# Compiler should give warning but doesn't?

Posted by Cristi Diaconescu on Stack Overflow See other posts from Stack Overflow or by Cristi Diaconescu
Published on 2010-04-29T21:27:31Z Indexed on 2010/04/29 21:37 UTC
Read the original article Hit count: 275

Filed under:
|
|
|

Someone on my team tried fixing a 'variable not used' warning in an empty catch clause.

try { ... } catch (Exception ex) { }

-> gives a warning about ex not being used. So far, so good.

The fix was something like this:

try { ... } catch (Exception ex) { string s = ex.Message; }

Seeing this, I thought "Just great, so now the compiler will complain about s not being used."

But it doesn't! There are no warnings on that piece of code and I can't figure out why. Any ideas?

PS. I know catch-all clauses that mute exceptions are a bad thing, but that's a different topic. I also know the initial warning is better removed by doing something like this, that's not the point either.

try { ... } catch (Exception) { }

or

try { ... } catch { }

© Stack Overflow or respective owner

Related posts about c#

Related posts about compiler