Is it possible to make this int const?

This is a discussion on Is it possible to make this int const? within the C++ Programming forums, part of the General Programming Boards category; >> Is it good style to catch exceptions "far away" from the place there they get thrown? Yes, sort of. ...

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> Is it good style to catch exceptions "far away" from the place there they get thrown?
    Yes, sort of. A common error made when using exceptions is to catch the exception too close to where it is thrown.

    The best place to catch an exception is wherever the best place to handle it is. In your case, invalid command line arguments should be handled in main (or the equivalent) so that the application can error out and exit.

    It would seem a little weird to catch boost::bad_lexical_cast somewhere far from where you do the cast, though.

    If you have multiple command line arguments to process, perhaps you can have a block of code that parses all of them and put that into it's own try/catch, but otherwise I think just leaving it where you have it (or around a main2() type function) is best.

  2. #17
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You can even do something as silly as
    Code:
    int main()
    try {
        //...
    }
    catch (boost::bad_lexical_cast&)
    {
        cout << "exception was thrown"
    }
    But that may indeed not be a good idea if you are not throwing the exceptions yourself, as you'd lose the ability to tell exactly what went wrong (multiple lexical_casts throwing).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #18
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Why not this?
    Code:
    int main( int argc, char* argv[] )
    {
       try
       {
          // Put all of the main() code in here.
       }
       catch ( whatever& e )
       {
          ...
       }
       catch ( blahblah& e )
       {
          ...
       }
       catch ( ... )
       {
          cout << "Unexpected exception caught!" << endl;
          return 1;
       }
    
       return 0;
    }

  4. #19
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    Since the ultimate goal here is the parsing of command line options, why not wrap all the option variables in a single class, make them const, and have main() call a function (passing argc and argv) which validates the arguments, builds the options object and returns it? If the options do not validate properly, throw an exception.

    Code:
    class program_options
    {
    public:
        program_options(int unit_val, int other_val, int foo_val, ...)
            : UNIT(unit_val),
              OTHER(other_val),
              FOO(foo_val)
              ...
        {
        }
    
        // Since the options are const, make them public, accessible directly
        const int UNIT;
        const int OTHER;
        const int FOO;
        ...
    };
    
    program_options get_program_options(int argc, char **argv)
    {
        if(options_not_valid())
        {
            throw program_option_exception();
        }
        ... 
       return program_options(unit_val, other_val, foo_val, ...)
    }
    
    int main(int argc, char **argv)
    {
        try
        {
            program_options opts = get_program_options(argc, argv);
            run_program(opts);
            return 0;
        }
        catch(program_option_exception e)
        {
            show_usage_message();
            exit(1);
        }
        return 0;
    }

  5. #20
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Thats a good solution and I'll probably try it, if I've a more complex option system to handle. for needs of heavy options parsing there's also boost::program_options. but I don't know if it is able to hold const values, too

  6. #21
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Well, it parses and then gives you values. You can store them in consts.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  4. Switch/case Problems (long code in post)
    By Wraithan in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2005, 05:40 PM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 06:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21