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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:59:10+00:00 2026-05-31T18:59:10+00:00

What is the difference between 2 conditions? Every time when method1 or method2 runs,

  • 0

What is the difference between 2 conditions? Every time when method1 or method2 runs, there should be a code block that is required to run. It seems to me that 2 method are the same.

// example method1
void Method1(void)
{
    try
    {
        // do something
    }
    catch (Exception ex)
    {
        // do something
    }
    finally
    {
        // do something whenever method1 runs
    }
}

// example method2
void Method2(void)
{
    try
    {
        // do something
    }
    catch (Exception ex)
    {
        // do something
    }

    // do something whenever method2 runs
}

Finally block seems to be unnecessary for me.

  • 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-31T18:59:12+00:00Added an answer on May 31, 2026 at 6:59 pm

    In your first example, you could re-throw the exception and the code inside the finally would still run. This would not be possible in the second example.

    If you choose not to re-throw the exception, then yes there is little difference. However, this is considered bad form – very rarely should you need to consume an exception that you cannot explicitly handle.

    It is a keyword to help you with code execution flow. When you throw an exception the execution flow of the code is affected (like using return), the finally keyword allows you to express that when an exception occurs (or you return from a try) you still want execution to do something as it’s leaving.

    To answer the question facetiously, it is a must when you need it and not when you don’t.


    Further Reading

    To be on the safe side, before you attempt to start making use of this keyword, please read the documentation for it:

    http://msdn.microsoft.com/en-us/library/zwc8s4fz.aspx

    And the exception handling keywords in general:

    http://msdn.microsoft.com/en-us/library/s7fekhdy.aspx


    Examples

    Catch an exception to do something with it, then re-throw it. Use finally to call any tidy-up code:

    try
    {
        OpenConnectionToDatabase();
        // something likely to fail
    }
    catch (Exception ex)
    {
        Log(ex);
        throw;  
        // throw ex; // also works but behaves differently
    }
    // Not specifying an exception parameter also works, but you don't get exception details.
    //catch (Exception)
    //{
    //    Log("Something went wrong);
    //    throw;
    //}
    finally
    {
        CloseConnectionToDatabase();
    }
    

    Don’t register any interest in catching exceptions, but use finally to tidy-up code:

    try
    {
        OpenConnectionToDatabase();
        // something likely to fail
    }
    finally
    {
        CloseConnectionToDatabase();
    }
    

    Return from your try because it looks nicely formatted, but still use finally to tidy-up code:

    try
    {
        OpenConnectionToDatabase();
        return 42;
    }
    finally
    {
        CloseConnectionToDatabase();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any difference between the following pieces of code in terms of processor
Is there any specific difference between these conditions? 1) begin acct = 1 return
Difference between a bus error and a segmentation fault? Can it happen that a
The difference between Chr and Char when used in converting types is that one
Is there any difference between int on_exit(void (*function)(int , void *), void *arg); and
Is there a performance difference between i++ and ++i if the resulting value is
Do you know the difference between these two conditions? 1 if(reader.hasrows()) { while(reader.read()) {
In many embedded applications there is a tradeoff between making the code very efficient
What is a difference between named_scope and named_scope + lambda Ruby on Rails code
Are there any difference between following ways to init self? First: - (id)init {

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.