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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:32:21+00:00 2026-05-23T15:32:21+00:00

Can someone enlighten me on handling the database connection (and errors) using try finally

  • 0

Can someone enlighten me on handling the database connection (and errors) using try finally ?
What would be the best practice ?
Seen various styles but I wonder what would be the best approach.
Should opening of the tables be put in TRY block or just the main connection
string ?
Since I usually put my database (absolute database,access..) in my exe folder
I was wondering about the best approach on this…
Or first check for file like …

if (FileExists(sDatabasePath)) then begin
    ADOConnection1.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+sDatabasePath+';Persist Security Info=False';
    try
        ADOConnection1.Connected:=True;
        ADOTable1.Open;
    except
        ShowMessage ('cant access the database !');
    end;
end;

???

  • 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-23T15:32:21+00:00Added an answer on May 23, 2026 at 3:32 pm

    Comments:

    • Never swallow exceptions, like you essentially do in your ShowMessage case. The code calling your procedure would have no way of knowing something went wrong. Only handle errors if you can fix them, or let them bubble-up the application error handler, where they’ll be displayed for the user.
    • Depending on how your code works, you might want to protect the connection to the database with a try-finally so you’re disconnected once the job is done. I don’t do that, I usually keep the connection open for the life of the application.
    • Depending on what you do with the ADOTable1 you might want to make sure it gets closed once you’re done using it with an other try-finally block. I usually do that because I don’t use Db aware GUI controls and I’m a control-freak. I also handle the transaction manually (start transaction / commit / rollback)
    • Don’t ignore errors. if your database doesn’t exist (ie: FileExists() returns false), code calling your procedure doesn’t know a thing, nor does the user.

    Here’s how I’d re-write your code:

    if (FileExists(sDatabasePath)) then 
      begin
          ADOConnection1.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+sDatabasePath+';Persist Security Info=False';
          ADOConnection1.Connected:=True;
          try
            ADOTable1.Open;
            try
              // Do non-GUI database stuff here.
            finally ADOTabel1.Close;
            end;
          finally ADOConnection1.Connected := False;
          end;
      end
    else
      raise Exception.Create('Database file not found');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone explain the mechanics of a jump table and why is would be
Can someone explain to me the advantages of using an IOC container over simply
Can someone explain what are the benefits of using the @import syntax comparing to
Can someone enlighten me on how to include an external java framework to android
Can someone enlighten on why is that oracle does not support an autoincrement feature
Can someone kindly enlighten me why the following code of mine does not work?
Can someone enlighten me what might be wrong in the following code? $wOwner =
Can someone enlighten me on this... When I uncomment the line in /etc/subversion/config store-auth-creds
Can someone show me how to implement a recursive lambda expression to traverse a
Can someone give an example of a good time to actually use unsafe and

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.