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 643651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:18:22+00:00 2026-05-13T21:18:22+00:00

I got a task to maintain a c# project, I found some code like

  • 0

I got a task to maintain a c# project, I found some code like this:

try{ 

    conn.Open(); 
    ... 
    conn.Close(); 
}catch(Exception e){ 

    conn.Close(); 
}

I know that the usual way to close SqlConnection is try-catch-finally or “using” keyword, but I found that I can’t prove the code above is wrong, I can’t find any situation that the Connection will be leak.

So could you give me some technical advises(not programming style) for it?Thanks

  • 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. Editorial Team
    Editorial Team
    2026-05-13T21:18:22+00:00Added an answer on May 13, 2026 at 9:18 pm

    You can’t find any situation? What about when that ... code contains a return statement? In a loop, continue and break can also fit the bill.

    Your particular code apparently doesn’t contain any control-flow statements, but that’s beside the point. The point is that when you use the common resource-acquisition idioms, such as try-finally and using, you no longer need to worry about what the rest of the code is. The ... can be one line or a hundred lines; whatever resource you acquired at the top will get released at the bottom if you’ve done it right. Rather than spend time trying to think of other ways that might be right (and having to prove it), use the ways you know are right because they were designed to be right.

    Besides, even if you can prove that the code never leaks a connection, it’s still a lousy idea for conn.Close() to be the command that has to solve every possible exception your program could ever throw. Don’t catch exceptions that you don’t know how to resolve.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer No, the ? will only stop the method being called… May 13, 2026 at 11:22 pm
  • Editorial Team
    Editorial Team added an answer You'll want to make a UserControl. There is a walkthrough… May 13, 2026 at 11:21 pm
  • Editorial Team
    Editorial Team added an answer For the sake of a simple example, say you want… May 13, 2026 at 11:21 pm

Related Questions

I've got a system I'm designing where we're using 4 specialized PCs we manufacture
I got a task related to ANCIENT C++ project which hasn't any documentation, comments
I've got a bunch of reports deployed as RDL's to a SSRS. Because of
I've got a ant build.xml that uses the <copy> task to copy a variety
I'm very new to SQL Server. I got a task from my boss where

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.