Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 30th, 2008, 08:38 PM
Friend of Wrox
 
Join Date: Sep 2007
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
Default Questions about Execption handling.

Hi

I am wondering when you use a using statement on say a stream Reader or any other stream do you need to surround with a try/catch statement? Or does the using statement take care of this?

Also how about if you use the File Stream. I know this is quite different then the other streams. So do you need to put this in a try/catch or a using statement?

 
Old September 30th, 2008, 10:00 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

The using statement is basically just a wrapper that does the same thing as a try...finally block. What it actually does in the finally block I'm not sure, but I think it calls the object's Dispose() method. Based on that and what the implementation of the Dispose method is on the object you are "using" you can decided whether or not to wrap it.

One recommendation: don't wrap code in try...catch if you aren't planning on doing something constructive in the catch. I have seen people wrap everything, everywhere in try...catch blocks only to simply rethrow the original exception. That is pointless. Just let the exception bubble up. It will be much easier to trouble shoot and debug if you see the line of code that threw the original and meaningful exception than to just see the error message on the line "throw ex;".

-Peter
compiledthoughts.com
 
Old October 1st, 2008, 08:28 AM
Friend of Wrox
 
Join Date: Sep 2008
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default

Peter has good advice here about not having empty catch blocks. My own choice is to return some kind of flag value from the routine that signals success (e.g., true) or failure (false) for the reader method. Also, it's generally not a good idea to put message box calls in the catch at this level. Message boxes are really part of the user interface and belong in the UI code. Use the return value from the reader call to decide what to do next.

Dr. Purdum


Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)





Similar Threads
Thread Thread Starter Forum Replies Last Post
.NET Interview Questions, C# Interview Questions, dotnetuncle .NET Framework 2.0 4 June 22nd, 2019 07:03 AM
Handling Cookies rit01 ASP.NET 2.0 Basics 6 March 22nd, 2006 04:21 PM
Datagrid handling calvados Pro VB.NET 2002/2003 1 December 6th, 2004 06:55 PM
Error Handling Louisa VB.NET 2002/2003 Basics 1 May 13th, 2004 10:22 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.