Errors and warnings


In chemfiles, any error will throw an exception. Any program using chemfiles should thus wrap any call in a try ... catch block, and handle the errors in any pertinent way.

#include <iostream>
#include "chemfiles.cpp"

int main() {
    try {
        chemfiles::Trajectory file("");
        chemfiles::Frame frame;;
        auto positions = frame.positions();
        // Do something here
    } catch (const chemfiles::Error& e) {
        // Basic error handling logging error to stdout
        std::cout << "Error in chemfiles:" << e.what() << std::endl;
        return -1;

    return 0;

Any exceptions thown by chemfiles will derive from chemfiles::Error. Catching chemfiles::Error will then catch any exception thown by chemfiles. You also can catch any other error if you need finer grain control. chemfiles::Error derives from std::runtime_error, so it should play nicely with any exisiting C++ error handling.

struct chemfiles::Error

Base exception class for chemfiles library.

Inherits from std::runtime_error

Subclassed by chemfiles::FileError, chemfiles::FormatError, chemfiles::MemoryError, chemfiles::SelectionError

struct chemfiles::FileError

Exception for files related failures.

Inherits from chemfiles::Error

struct chemfiles::MemoryError

Exception for memory related failures.

Inherits from chemfiles::Error

struct chemfiles::FormatError

Exception for formats related failures.

Inherits from chemfiles::Error

struct chemfiles::SelectionError

Exception for errors in selections.

Inherits from chemfiles::Error


Chemfiles also send warnings on some malformed files. You can use the set_warning_callback function to register a global callback to use when sending a warning.

void chemfiles::set_warning_callback(warning_callback callback)

Set the global callback for warning events. The default is to print them on the standard error stream.

typedef std::function<void(std::string message)> chemfiles::warning_callback

Callback type used to process a warning event.