Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 130739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:59:44+00:00 2026-05-11T05:59:44+00:00

Given this code: using (var conn = new SqlConnection(…)) { conn.Open(); using (var cmd

  • 0

Given this code:

using (var conn = new SqlConnection('...')) {     conn.Open();     using (var cmd = conn.CreateCommand())     {         cmd.CommandText = '...';         using (var reader = cmd.ExecuteReader())         {             while (reader.Read())             {                 // ...             }         }     } } 

I’m used to writing try/catch/finally blocks for my data access, however, I’m being exposed to ‘using’ which seems like a much simpler method of doing this. However, I’m trying to figure out how to catch exceptions that may occur.

Could you please give me an example of how you’d catch exceptions?

Edited to add:

I’m being led to believe that ‘using’ is a replacement for my try/catch/finally blocks. I understand that using doesn’t catch exceptions. So how is this a replacement?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T05:59:44+00:00Added an answer on May 11, 2026 at 5:59 am

    using isn’t designed to catch exceptions; it’s designed to give you an easy way to wrap a try/finally around an object that needs to be disposed. If you need to catch and handle exceptions then you’ll need to expand it into a full try/catch/finally or put a containing try/catch around the whole thing.


    To answer your edit (is using a replacement for try/catch/finally?) then no, it isn’t. Most of the time when using a disposable resource you aren’t going to handle the exception there and then because there’s normally nothing useful you can do. So it provides a convenient way to just ensure that the resource is cleaned up irrespective of what you’re trying to do works or not.

    Typically code that deals with disposable resources is working at too low a level to decide what the correct action is on failure, so the exception is left to propagate to the caller who can decide what action to take (e.g. retry, fail, log, etc.). The only place where you’d tend to use a catch block with a disposable resource is if you’re going to translate the exception (which is, I assume, what your data access layer is doing).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 129k
  • Answers 129k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer $("#sortable").append("<li class='ui-state-default'>"+ $("#ContentList option:selected").text()+"</li>"); $("#ContentList option:selected").remove(); should do the trick...… May 12, 2026 at 5:56 am
  • Editorial Team
    Editorial Team added an answer You would use the Wordpress functions is_single() and is_page. if(!is_single()… May 12, 2026 at 5:56 am
  • Editorial Team
    Editorial Team added an answer By 'activate', do you mean make it so the user… May 12, 2026 at 5:56 am

Related Questions

Given this code: using (var conn = new SqlConnection(...)) { conn.Open(); using (var cmd
Given the following code: using (var client = new WebClient()) { string url =
Given this XML ... <ListBucketResult xmlns=http://s3.amazonaws.com/doc/2006-03-01/> <Name>public.rpmware.com</Name> <Prefix></Prefix> <Marker></Marker> <MaxKeys>1000</MaxKeys> <IsTruncated>false</IsTruncated> <Contents> <Key>0.dir</Key> <LastModified>2008-06-25T16:09:49.000Z</LastModified>
Possible Duplicate: LINQ to SQL: Return anonymous type? I have a standard LINQ to
I've just started using NHibernate, and I have some issues that I'm unsure how

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.