Geeks With Blogs
Eron Wright - All Killer No Filler blog 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 ......

Posted On Saturday, January 3, 2004 11:32 AM

Copyright © Eron Wright | Powered by: GeeksWithBlogs.net