Blog Stats
  • Posts - 24
  • Articles - 0
  • Comments - 72
  • Trackbacks - 91

 

January 2004 Entries

Anti-pattern: incorrect try..finally usage

I am so sick of seeing this incorrect usage of try..finally: Stream s = null; try { s = new FileStream(...); ... } finally { if(s!=null) s.Close(); } This is WRONG. The work done in the finally block need only be done if the stream is opened. Until the assignment of 's' completes, the finally block is not to be executed. The 'if' statement is basically checking that fact. The correct usage is: Stream s = new FileStream(...); try { ... } finally { s.Close(); } No excuses! Programmers are lucky that...

 

 

Copyright © Eron Wright