Handling Errors

This topic describes how to handle AVBlocks errors.

When a method encounters an error during its execution, it throws a TAVBlocksException. The TAVBlocksException::error method returns a TErrorInfo object with the details. The open method of TMediaInfo and TTranscoder also has a tryOpen variant that returns false instead of throwing. In that case you can get the TErrorInfo object by calling the error method of the corresponding class, for example TMediaInfo::error or TTranscoder::error.

The code snippets in this article are from the info_stream_file macOS sample.

This example shows how you might handle an error if the TMediaInfo::tryOpen method did not succeed:

bool avInfo(Options& opt)
{
    TMediaInfo mi;
    mi.inputs(0).file(opt.inputFile);

    if (mi.tryOpen())
    {
        printStreams(mi);
        return true;
    }
    else
    {
        printError("MediaInfo open", mi.error());
        return false;
    }
}

The TErrorInfo::facility, TErrorInfo::code, and the TErrorInfo::message methods return the component in which the error happened, the error code, and the error message. In addition for most errors the TErrorInfo::hint method may provide additional useful information.

The following example shows how you can use the TErrorInfo object:

inline void printError(const char* action, const primo::avblocks::modern::TErrorInfo& e)
{
    using namespace std;

    if (action)
        cout << action << ": ";

    if (e.facility() == primo::error::ErrorFacility::Success)
    {
        cout << "Success" << endl;
        return;
    }

    if (!e.message().empty())
        cout << e.message() << ", ";

    cout << "facility:" << e.facility()
         << ", error:" << e.code()
         << ", hint:" << e.hint()
         << endl;
}