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

  • SEARCH
  • Home
  • 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 5970661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:24:25+00:00 2026-05-22T20:24:25+00:00

This is a question of what the ‘best practice’ is for declaring new variables,

  • 0

This is a question of what the ‘best practice’ is for declaring new variables, and I’ve seen this situation a few times now. I have a class whose constructor reads a config file, eg:

ConfigMgr config = new ConfigMgr(args[0]);

Of course, if you run the console app without that argument, an exception results. If I surround that line with a try/catch as follows, I get the error ‘The name ‘config’ does not exist in the current context’. Understandable.

try
{
    ConfigMgr config = new ConfigMgr(args[0]);
}
catch
{
    Console.WriteLine("Config file not specified or incorrect in format.  Exiting.");
    Console.ReadLine();
}

// Defaults
string aucomposingfile = config.getValue("aucomposingfile");
string nzcomposingfile = config.getValue("nzcomposingfile");
...etc

I could separate out the part that requires the argument in the constructor – do the new ConfigMgr part outside the try/catch block, then do something like config.LoadFile() in the try/catch. But I can’t imagine that’s what those in the know do.

Any thoughts?

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-22T20:24:26+00:00Added an answer on May 22, 2026 at 8:24 pm

    There is no reason why you cannot do this:

    ConfigMgr config = null;
    try
    {
        config = new ConfigMgr(args[0]);
    }
    catch ( /* catch a specific exception!! */ ) 
    {
        //log it:
        Console.WriteLine("Config file not specified or incorrect in format.  Exiting.");
    
        //escape from here, because you don't want to continue:
        throw;    
    }
    
    string aucomposingfile = config.getValue("aucomposingfile");
    string nzcomposingfile = config.getValue("nzcomposingfile");
    

    Of course the arguably more correct way may be to check the command line arguments before attempting to use them, and throw a specific exception if they don’t meet your requirements – you depend on them, so verify them first. This has a better code flow than just trying to use them and catching an exception. This way your catch block becomes more focused on problems more specific to the config file, rather than handling issues with the command line arguments as well.

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

Sidebar

Related Questions

This question was very helpful, however I have a list control in my report,
This question arose when I was working on answering another question about best practices
This question has been asked a lot of times in many forums. But I
This question is just for my better understanding of static variables in C++. I
This question seems to have been asked a lot but all were trying to
This question is specific to Erlang, but may have general implications to other IO
This question would not have existed if AspectJ worked the same way as EJB
This question is not as simple as it seems to be. I have 64
This question has been brought up before, and I have searched many of the
This question is best explained with a screenshot: http://i42.tinypic.com/2ccvx91.jpg The wrapper div has a

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.