Catching errors of complex functions.

Greetings.

I'm currently processing data stored in about 100000 files. While I've written the code which processes the data once loaded, I'm using an open source library to extract the data from the files. The open source code nearly always works but occasionally hits a file it can't extract data from - which the open source code is fine with but crashes the rest of the function that code is housed in.

My program works by reading the names of the files to be processed from a list and repetitively passing the name of the next file from the list to a custom function housing the open source code and other processing information.

I'd like to know if there is a way to check if the function call crashes (or fails) and print the name of the file being passed to the function when the failures occur - while continuing on with the next round of input regardless. I know there are various try-catch structures available in c++ but I've never really used them (and never for a code which was meant to catch any errors)

Code (with irrelevant bits cut out) looks like this:


1
2
3
4
5
6
7
8
9
ifstream pdbFileReader;
string currentFile;

pdbFileReader.open("/selvan/fileLocations.txt");

while (pdbFileReader >> currentFile)
{
ExtractDataFromFile(currentFile);
}



The errors always occur within my function ExtractDataFromFile - which is fairly complex and does several things. Is there some try-catch I can put around the function call above to ignore the call if it crashes and print the value of "currentFile" to screen when it does? It would make deleting the unworkable files from my list much faster then the approach I'm currently using.
Does it throw an exception that you can catch? If it thrown an exception, you could catch it so that it doesn't crash the whole program. Having to start processing that many files again is a huge loss of time.

You could certainly wrap it in an a try-catch. If it throws any exceptions, you could catch them and prevent your program from crashing.
I don't know if the function throws errors. The open source software does cause an error message to be displayed (stating the piece of data in the file it couldn't extract) but I'm not sure what or how to check for that statement.

Is there a way to check if the function is throwing any error messages from the piece of code I wrote above?

This code will catch exceptions thrown from ExtractDataFromFile. If whatever is going wrong isn't actually an exception being thrown, it won't do you any good.

1
2
3
4
5
6
7
8
9
10
11
12
13
try
{
  ExtractDataFromFile(currentFile);
}
catch (const std::exception& ex)
{
    cout << "Caught standard exception when dealing with file " << currentFile << endl;
   cout <<  "Exception says: " << ex.what() << endl;
}
catch ( ... )
{
  cout << "Caught unknown exception when dealing with file " << currentFile << endl;
}

This code will then continue, so the loop will keep running. There are no guarantees about the state that things will have been left in.
Last edited on
I tried the structure above and the program still stops running at the same point. Which seems odd to me because I do seem to get an error message printed out with or without the try-catch.

I'm running my program on a remote Linux cluster and the outputs of the program are saved to a .o (output) file and a .e (error) file. Running the program does result in an entry being made to the .e file which reads:

Fatal error: Cannot extract field 'charge' in
<|HETATM11383 ZN ZN A 801 67.588 68.008 -5.185 1.00 25.73 ZN+2|>

Which definitely originates from the function in question.

As a final measure, could anyone tell me if there is a way to cause my function to return an error (or purposefully fail) in a way Moschops' code would pick up? I'd like to test I set up the try-catch correctly before assuming I can't catch this fault out .
Fatal error: Cannot extract field 'charge' in

Fatal error usually means "We're calling exit right after we output this error message."

I would consult the documentation for whatever open source library you're using and see if you can adjust the behavior of the code.
Was hoping even Fatal errors could be ignored by try-catch blocks.

I'll check the documentation again but I might just sift through all the problem files manually. I've spent so much time trying to find a smart way of doing this once off job, I'm thinking it will be more efficient to just brute force my way through.

Thanks for the many speedy replies. I really appreciate them.

Topic archived. No new replies allowed.