Results 1 to 6 of 6

Thread: Am I using try catch finally right?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    Am I using try catch finally right?

    Hi Guys,

    I am trying to cover all my bases here with an application I am creating. As it will be released to the general community I have to account for user error so I am implementing structured error handling. What I want to know is:

    A: am I using this correctly
    B: if I but the finally block at the end of a method does it skip over any code after the catch block and close the method?

    Code:
            private void OpenFileForReading(string Path)
            {
                try
                {
                    stream = new FileStream(Path, FileMode.Open);
                    reader = new BinaryReader(stream);
                }
    
                catch (Exception e)
                {
                    ListOfErrors.Add(e.ToString());
                }
    
                finally
                {
                    reader.Close();
                    reader = null;
    
                    stream.Close();
                    stream = null;
                }
            }

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Am I using try catch finally right?

    in the finally, you should first check 1) that the object exists.. and 2) that it is open.... then close it. if the error happens because the stream couldn't be opened... then the object is likely to be null... which means 1) you can't call close on your stream and 2) your reader will have never been created, so it too will be null.

    In regards to B ... Try catch finally have to appear together... you can't have extraneous code between the Catch and Finally (or at least I don't think you can, nor would that make sense.)

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    Re: Am I using try catch finally right?

    Ah yes that would make sense for A. as for B so everything should be in the Try Part?

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Am I using try catch finally right?

    That's up to you... generally most people like to try to keep their try blocks as small as possible... but it depends on how granular you want to trap for errors. Obviously try every line one by one would not only be tedious, but over kill. By the same token you don't want a 100-line Try blck where a dozen different things could go wrong either. since you are closing your streams in the finally, obviously, anything that comes after that isn't going to have the streams open... There's no clear answer. Actually there is: It depends.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Am I using try catch finally right?

    'finally' blocks are redundant in many cases these days. If all you're doing in a 'finally' block is disposing objects then you should probably be using a 'using' block, no pun intended.
    Code:
            private void OpenFileForReading(string Path)
            {
                try
                {
                    using (stream = new FileStream(Path, FileMode.Open))
                    {
                        using (reader = new BinaryReader(stream))
                        {
                            // ...
                        }
                    }
                }
                catch (Exception e)
                {
                    ListOfErrors.Add(e.ToString());
                }
            }
    No need to null your variables because they are scoped by the 'using' block and no need to test for null before disposing because disposal is implicit.

    Note that 'using' is only for objects that implement IDisposable, so you would still need to use a 'finally' block where there was work other than disposing objects to do.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    Re: Am I using try catch finally right?

    Cheers Guys!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width