throw or print error
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:
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?
Originally Posted by l2u
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).
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?
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.
Originally Posted by Elysia
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.
Read file code:
Call function to read specific setting.
If error, push back exception and move on.
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.