Results 1 to 6 of 6

Thread: Difference with using {} and try{} Catch{}

  1. #1

    Thread Starter
    Addicted Member effekt26's Avatar
    Join Date
    Nov 2006
    Posts
    138

    Difference with using {} and try{} Catch{}

    Hi Everyone,

    Just a quick question, what is the difference between the using statement and the Try, Catch block?

    i know that i have to manually dispose of the connection and command objects in the finally statement, but i always put something in the catch block, for error handling.

    how is this error handling used in the using block?

    Cheers,
    Justin

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

    Re: Difference with using {} and try{} Catch{}

    The 'using' statement negates the need for a 'finally' block by guaranteeing that the object created will be disposed no matter what occurs inside the 'using' block itself. The 'using' block provides no error handling mechanism, but the object created by the using statement will always be disposed when the block execution exits the block, no matter how that happens. That includes an exception being thrown and not caught. In short, this:
    C# Code:
    1. using (IDosposable obj = new IDisposable())
    2. {
    3.     try
    4.     {
    5.  
    6.     }
    7.     catch
    8.     {
    9.  
    10.     }
    11. }
    is equivalent to this:
    C# Code:
    1. IDosposable obj = new IDisposable();
    2.  
    3. try
    4. {
    5.  
    6. }
    7. catch
    8. {
    9.  
    10. }
    11. finally
    12. {
    13.     obj.Dispose();
    14. }
    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

  3. #3

    Thread Starter
    Addicted Member effekt26's Avatar
    Join Date
    Nov 2006
    Posts
    138

    Re: Difference with using {} and try{} Catch{}

    Hi jmcilhinney,

    Thanks for the info, i was aware of both, but was not aware you could use both together, but it makes sense.

    Cheers, and thanks for all your help.

    Justin

  4. #4

    Thread Starter
    Addicted Member effekt26's Avatar
    Join Date
    Nov 2006
    Posts
    138

    Re: Difference with using {} and try{} Catch{}

    Hi Again,

    Say if i had to work with 2 or 3 different streamreaders/writers in the same void, could i possibly do this:

    Code:
    public TransLog() {
    
                try {
    
                    string _date = DateTime.Now.ToShortDateString();
                    string _fileName = Environment.CurrentDirectory + "\\logs\\translog_" + _date;
    
    
                    if (File.Exists(_fileName) == true) {
    
                        using (FileStream _fileStream = new FileStream(_fileName, FileAccess.ReadWrite)) {
    
                        }
    
    
                    } else {
    
                    }
    
                } catch (Exception ex) {
    
                }
            
            }
    and the catch statement would still catch any exceptions that could occur within the using the statement...

    or would i need to do something like this, and add try{} catch{} to each of my using{} statements:
    Code:
    public TransLog() {
    
                try {
    
                    string _date = DateTime.Now.ToShortDateString();
                    string _fileName = Environment.CurrentDirectory + "\\logs\\translog_" + _date;
    
    
                    if (File.Exists(_fileName) == true) {
    
                        using (FileStream _fileStream = new FileStream(_fileName, FileAccess.ReadWrite)) {
    
                            try {
    
                            } catch (Exception ex) {
    
                            }
    
                        }
    
    
                    } else {
    
                    }
    
                } catch (Exception ex) {
    
                }
            
            }
    and the top level try{} catch{} is there to catch any errors outside the using block...

    Thanks, Justin

  5. #5
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Difference with using {} and try{} Catch{}

    Give it a shot.

    Your first one will work.

    c# Code:
    1. // Catch exceptions
    2.         private void button1_Click(object sender, EventArgs e)
    3.         {
    4.             try
    5.             {
    6.                 using // Whatever
    7.                 {
    8.                     int x = 0;
    9.                     int y = 100;
    10.                     int t = y / x;
    11.                 }
    12.             }
    13.             catch (Exception ex)
    14.             {
    15.                 MessageBox.Show(ex.Message);
    16.             }
    17.         }
    Last edited by nmadd; Jun 28th, 2007 at 10:36 AM. Reason: added c# tags

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

    Re: Difference with using {} and try{} Catch{}

    I wouldn't say that Using negates the use of Finally.... it does if all the finally does is .Dispose the object.

    Rather the Using negates the need to explicitly create the object and dispose of it at the end.

    -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??? *

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