throw or print error

This is a discussion on throw or print error within the C++ Programming forums, part of the General Programming Boards category; Hello I got stuck in situation where I parse my application's configuration file and I'm unsure if I should throw ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    throw or print error

    Hello

    I got stuck in situation where I parse my application's configuration file and I'm unsure if I should throw an exception or just print the error and continue with parsing if there is bad setting in configuration file.

    I want to be able to reload the configuration file during the application runtime.

    For instance if I have website-list option in my configuration file like:

    website: www.google.com
    website: 233^˘˘^\ˇ\
    website: www.yahoo.com

    should the program throw when it comes to parse the second website or should it continue with parsing and log that theres a bad website argument in configuration file?

    Thanks a lot for help.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by l2u View Post
    Hello

    I got stuck in situation where I parse my application's configuration file and I'm unsure if I should throw an exception or just print the error and continue with parsing if there is bad setting in configuration file.

    I want to be able to reload the configuration file during the application runtime.

    For instance if I have website-list option in my configuration file like:

    website: www.google.com
    website: 233^˘˘^\ˇ\
    website: www.yahoo.com

    should the program throw when it comes to parse the second website or should it continue with parsing and log that theres a bad website argument in configuration file?

    Thanks a lot for help.
    How do we know?

    My suggestion is, if going on makes sense at all, you should go on. How you note that there's a bad URL/whatever in the configuration file depends on how it got there (do you expect people to edit it themselves, or was it written by your program?) and how serious the error it is (it sounds like, in this case, not that serious).

  3. #3
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    The configuration file is user-configureable. When does it make a sense to go on? I guess in this case it makes a sense to go on - so I shouldnt throw?

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    Do not let a utility function print errors - that much I can say.
    I would suggest reading on and the function returns the result of the reading - say, it could only read half the file because the were some errors, so the main code could print the error and notify the user.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by Elysia View Post
    Do not let a utility function print errors - that much I can say.
    I would suggest reading on and the function returns the result of the reading - say, it could only read half the file because the were some errors, so the main code could print the error and notify the user.
    Yeah but the problem is with a function that reads website list, and when it reads the first website which doesnt match the regex, it should go on to the second and if the second doesnt match it, still goes on to the third.. Then it can return -1 or "bad_website" but it cant tell more specific that the first and the second website didnt match the regex.

    I think it would be best to return false if there was an error, and print all the errors during the parsing (to the log file) so the user could knew exactly what went wrong.

    What do you think?

    On the other side, throwing exception can only tell that the first website didnt match and it will stop parsing.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    Main code:
    Read file code:
    Call function to read specific setting.
    If error, push back exception and move on.
    Repeat.
    Return exceptions.

    This might be a viable approach. Logging is also a good idea, but the user would likely want to know something went wrong, and what.
    Btw, by exception, I don't necessarily mean it throws. It can return just as well.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  4. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  5. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM

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