2.4.1. uhd_safe_main.hpp#

2.4.1.1. Source code#

 1//
 2// Copyright 2010 Ettus Research LLC
 3// Copyright 2018 Ettus Research, a National Instruments Company
 4//
 5// SPDX-License-Identifier: GPL-3.0-or-later
 6//
 7
 8#pragma once
 9
10#include <uhd/config.hpp>
11#include <iostream>
12#include <stdexcept>
13
14/*!
15 * Defines a safe wrapper that places a catch-all around main.
16 * If an exception is thrown, it prints to stderr and returns.
17 * Usage: int UHD_SAFE_MAIN(int argc, char *argv[]){ main code here }
18 * \param _argc the declaration for argc
19 * \param _argv the declaration for argv
20 */
21#define UHD_SAFE_MAIN(_argc, _argv)                               \
22    _main(int, char* []);                                         \
23    int main(int argc, char* argv[])                              \
24    {                                                             \
25        try {                                                     \
26            return _main(argc, argv);                             \
27        } catch (const std::exception& e) {                       \
28            std::cerr << "Error: " << e.what() << std::endl;      \
29        } catch (...) {                                           \
30            std::cerr << "Error: unknown exception" << std::endl; \
31        }                                                         \
32        return ~0;                                                \
33    }                                                             \
34    int _main(_argc, _argv)