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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:31:46+00:00 2026-05-27T15:31:46+00:00

having this code, I don’t understand why if assigning a variable in a finally

  • 0

having this code, I don’t understand why if assigning a variable in a finally block doesn’t understand it will ALWAYS be assigned. I think I missing a valid option where currency won’t be assigned. If you know, will be great to understand why. much appreciate it!

Thanks!

CurrencyVO currency;

try
{
     if (idConnection.HasValue && idConnection != 0)
     {
         currencyConnection = client.GetConnection(idConnection.Value);
         model.Connection = currencyConnection;
     }
     else 
     {
         int providerUserKey = (int)Models.UserModel.GetUser().ProviderUserKey;
         currencyConnection = client.GetConnection(providerUserKey);
     }                        
     currency = model.Currencies.SingleOrDefault(c => c.IdCountry == currencyConnection.idcountry) ?? new CurrencyVO();    
} 
catch
{
      currency = new CurrencyVO();                    
} 
finally
{
      model.PublishedContainer.Currency = currency;
}

the error happens on the finally block. If i take it out of the finally block like this :

                } catch {
                    currency = new CurrencyVO();
                }
                model.PublishedContainer.Currency = currency;

it works fine.

  • 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-27T15:31:47+00:00Added an answer on May 27, 2026 at 3:31 pm

    The definite assignment tracking that the C# compiler performs doesn’t necessarily perform a complete analysis (that wouldn’t be possible in the general case) – there are rules that restrict how complex of an analysis the compiler will perform. The rule covering the finally block here is documented at http://msdn.microsoft.com/en-us/library/aa691181.aspx:

    For a try statement stmt of the form:

    try try-block finally finally-block

    • The definite assignment state of v at the beginning of try-block is the same as the definite assignment state of v at the
      beginning of stmt.
    • The definite assignment state of v at the beginning of finally-block is the same as the definite assignment state of v at
      the beginning of stmt.
    • …

    So for your particular example, since currency is not definitely assigned at the beginning of the try block, it is considered to be not definitely assigned at the beginning of the finally block.

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

Sidebar

Related Questions

as all you know $(#ID) returns the element having ID. but this code always
I don't know why I'm having this error. Here is the code where i
I'm having real trouble with this; I don't know how to make this code
i'm having this issue with VBO's that I don't really understand. My problem is
Having this code: using (BinaryWriter writer = new BinaryWriter(File.Open(ProjectPath, FileMode.Create))) { //save something here
Having this code... var b = new ReadOnlyCollection<int>(new[] { 2, 4, 2, 2 });
I'm having trouble with this code: NSRect itemFrame; id item; // code to assign
I've been having problems with this code I had spent the last 3 hours
When declaring a template, I am used to having this kind of code: template
I am having some trouble with this code . The problem is when i

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.