In chemfiles, the errors are handled by using exceptions. So any program using
chemfiles should wrap the call between a try ... catch statment, and handle the
errors in any pertinent way.
#include <iostream>
#include "chemfiles.cpp"
int main() {
try {
chemfiles::Trajectory file("filename.xyz");
chemfiles::Frame frame;
file.read(frame);
auto positions = frame.positions();
// Do something here
} catch (const chemfiles::Error& e) {
// Basic error handling by just logging error to stdout
std::cout << "Error in chemfiles:" << e.what() << std::endl;
return -1;
}
return 0;
}
All the exceptions derives from chemfiles::Error, so catching only
chemfiles::Error is OK. You also can catch any other error if you need finer
grain control. chemfiles::Error derives from std::runtime_error, and thus
should play nicely with any exisiting C++ error handling.
chemfiles::ErrorBase exception class for chemfiles library.
chemfiles::FileErrorException for files related failures.
chemfiles::MemoryErrorException for memory related failures.
chemfiles::FormatErrorException for formats related failures.
chemfiles::PluginErrorException for Molfile plugins errors.
chemfiles::SelectionErrorException for errors in selections.