Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
ExceptionUtils.h
Go to the documentation of this file.
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* ExceptionUtils.h (C) 2000-2025 */
9/* */
10/* Utility functions for exception handling. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_COMMON_EXCEPTIONUTILS_H
13#define ARCCORE_COMMON_EXCEPTIONUTILS_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
18#include "arccore/common/CommonGlobal.h"
19
20#include <functional>
21
22/*---------------------------------------------------------------------------*/
23/*---------------------------------------------------------------------------*/
24
25namespace Arcane::ExceptionUtils
26{
27
28/*---------------------------------------------------------------------------*/
29/*---------------------------------------------------------------------------*/
30
31/*!
32 * \brief Prints a message for an unknown exception.
33 *
34 * This function is used for `catch(...)` expressions.
35 *
36 * If \a trace_mng is not null, it will be used for printing.
37 * If \a is_no_continue is true, it displays a message indicating that execution cannot
38 * continue.
39 *
40 * \retval 1
41 */
42extern "C++" ARCCORE_COMMON_EXPORT Int32
43print(ITraceMng* tm, bool is_no_continue = true);
44
45/*---------------------------------------------------------------------------*/
46/*---------------------------------------------------------------------------*/
47
48/*!
49 * \brief Prints a message for the standard exception \a ex.
50 *
51 * If \a trace_mng is not null, it will be used for printing.
52 * If \a is_no_continue is true, it displays a message indicating that execution cannot
53 * continue.
54 *
55 * \retval 2
56 */
57extern "C++" ARCCORE_COMMON_EXPORT Int32
58print(const std::exception& ex, ITraceMng* tm, bool is_no_continue = true);
59
60/*---------------------------------------------------------------------------*/
61/*---------------------------------------------------------------------------*/
62
63/*!
64 * \brief Prints a message for the standard exception \a ex.
65 *
66 * If \a tm is not null, it will be used for printing.
67 * If \a is_no_continue is true, it displays a message indicating that execution cannot
68 * continue.
69 *
70 * \retval 3
71 */
72extern "C++" ARCCORE_COMMON_EXPORT Int32
73print(const Exception& ex, ITraceMng* tm, bool is_no_continue = true);
74
75/*---------------------------------------------------------------------------*/
76/*---------------------------------------------------------------------------*/
77
78/*!
79 * \brief Calls a function while catching and displaying exceptions.
80 *
81 * Executes the function \a function and catches any exceptions.
82 * In case of an exception, the print() function is called to display a
83 * message, and the return code is that of the print() function.
84 *
85 * Usage:
86 *
87 * \code
88 * callWithTryCatch([&]() { std::cout << "Hello\n"});
89 * \endcode
90 *
91 * \return 0 if no exception is thrown, and a positive value otherwise.
92 */
93extern "C++" ARCCORE_COMMON_EXPORT Int32
94callWithTryCatch(std::function<void()> function, ITraceMng* tm = nullptr);
95
96/*---------------------------------------------------------------------------*/
97/*---------------------------------------------------------------------------*/
98
99/*!
100 * \brief Calls a function and terminates the program if an exception occurs.
101 *
102 * Calls the function \a function via callWithTryCatch() and calls
103 * std::terminate() in case of an exception.
104 */
105extern "C++" ARCCORE_COMMON_EXPORT void
106callAndTerminateIfThrow(std::function<void()> function, ITraceMng* tm = nullptr);
107
108/*---------------------------------------------------------------------------*/
109/*---------------------------------------------------------------------------*/
110
111} // namespace Arcane::ExceptionUtils
112
113/*---------------------------------------------------------------------------*/
114/*---------------------------------------------------------------------------*/
115
116#endif
Declarations of types for the 'base' component of Arccore.
Int32 print(ITraceMng *tm, bool is_no_continue=true)
Prints a message for an unknown exception.
void callAndTerminateIfThrow(std::function< void()> function, ITraceMng *tm=nullptr)
Calls a function and terminates the program if an exception occurs.
Int32 callWithTryCatch(std::function< void()> function, ITraceMng *tm=nullptr)
Calls a function while catching and displaying exceptions.
std::int32_t Int32
Signed integer type of 32 bits.