I have an service application that reads a config file containing a list of files to open. The problem is when it doesn’t find the file in the directory, it throws an exception thus cancelling the thread and stopping the service application as well.
Here’s a code block of the function being called in a thread:
FILE* file_;
ServiceApp::File::File( const char* filename, const char* mode) :
file_(fopen(filename, mode))
{
if( !file_ )
{
// throwing will stop the service when file doesn't exist, what work around could we do?
throw std::runtime_error("file open failure");
}
Question:
How do we prevent this from happening so that when a file listed in the config file is not found in the directory, the application would just ignore it and continue with the process?
The easiest would be just to try-catch your own exception, in the thread, then check for it being that exception (to ignore it), and rethrowing if it’s some other exception.
I’m not sure if it’s possible to add info to the exception where thrown or is it legacy code? In the latter case you’ll have to resort to string comparison, which of course is ugly. Anyway.
On Rethrowing