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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:05:04+00:00 2026-06-11T23:05:04+00:00

Two very basic questions about exception handling in Delphi. 1) When to Try ?

  • 0

Two very basic questions about exception handling in Delphi.

1) When to Try? My guess is that I don’t need a Try clause around

  • strightforward code such as assignments, conditionals and loops
  • access to my VCL compnents

but that I do need to Try

  • database access
  • any thrid party components, as I don’t know if they might raise an exception or not
  • anything which the help system shows can raise an exception

Did I miss anything?

2) Try … Finally or Try … Except … or both?
For years I have thought this to be an either / or choice, until @RRUZ answered one of my questions with some code which went

 try
    CoInitialize(nil);
    try
      SetStaticIpAddress('Network card name','192.168.1.1','255.255.255.0');
    finally
      CoUninitialize;
    end;
 except
    on E:EOleException do
        Writeln(Format('EOleException %s %x', [E.Message,E.ErrorCode]));
    on E:Exception do
        Writeln(E.Classname, ':', E.Message);
 end;

Question: is that except only going to catch exceptions from CoInitialize(nil); or also from SetStaticIpAddress('Network card name','192.168.1.1','255.255.255.0');?

To put it another way, is it possible to have my cake and eat it by having a bested try finally within a try except?


[update] the answer to #2 seems to be yes. This code shows both dialog boxes …

procedure TForm3.FormCreate(Sender: TObject);
  var x, zero : Integer;
begin
   zero := 0;
   try
      try
        x := 42 div zero;
      finally
         MessageDlg('Divide by zero finally', mtInformation, [mbOK], 0);
      end;

   Except
     on E: Exception do
     MessageDlg('Divide by zero exception handled', mtInformation, [mbOK], 0);
   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-06-11T23:05:06+00:00Added an answer on June 11, 2026 at 11:05 pm

    While they both pertain to exception handling they are different beasts.

    Try…Finally is for resource cleanup. It should always be used when you allocate resources that get cleaned up at the end of the routine. (Interpret “resources” broadly here–you need it for things like locks etc. also.)

    Try…Except is for catching exceptions. Use it only when there is an exception that could happen that you have a reasonable way of handling. You should almost never simply grab all exceptions other than as part of a top-level error logging facility. (I won’t say you should never catch all–for example, you’re reading a config file and it’s bad. Your only real choices are abort the program or squawk, use defaults and continue. In general users would probably prefer the latter.)

    They can be nested to any depth (when you’re allocating multiple resources you either must nest them or you must have some way of figuring out if a resource was obtained or not before letting go of it) and coexist freely.

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

Sidebar

Related Questions

This might be a very basic question but it confuses me. Can two different
I have a very basic app with two ignition-remoteimageviews (RIV) in them. The app
I have a very basic page with two elements, an outer green box, and
I have two very simple, identical UITableViews in my app that are populated with
I am building a web application using ASP.NET MVC that has two very distinct
Since I am new to Java, I need some help about some basic things
I'm having a very basic question about regular expressions. I am trying to match
I've got, probably, a very basic question about sessions. In the page load function
I have a very basic question about binding between controls. I have a class
A very basic question it is but I wanted expert advice that is why

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.