Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
ExceptionUtils.cc
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.cc (C) 2000-2025 */
9/* */
10/* Utility functions for exception handling. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
15
16#include "arccore/base/Exception.h"
17#include "arccore/trace/ITraceMng.h"
18
19#include <iostream>
20
21/*---------------------------------------------------------------------------*/
22/*---------------------------------------------------------------------------*/
23
29
30/*---------------------------------------------------------------------------*/
31/*---------------------------------------------------------------------------*/
32
33namespace Arcane
34{
40
41/*---------------------------------------------------------------------------*/
42/*---------------------------------------------------------------------------*/
43
44namespace
45{
46 const char* _noContinueString(bool is_no_continue)
47 {
48 return (is_no_continue) ? "** Can't continue with the execution.\n" : "";
49 }
50} // namespace
51
52/*---------------------------------------------------------------------------*/
53/*---------------------------------------------------------------------------*/
54
55Int32 ExceptionUtils::
56print(ITraceMng* msg, bool is_no_continue)
57{
58 const char* nc = _noContinueString(is_no_continue);
59 const char* msg_str = "** An unknowed error occured...\n";
60 if (msg) {
61 msg->error() << msg_str << nc;
62 }
63 else {
64 std::cerr << msg_str << nc;
65 }
66 return 1;
67}
68
69/*---------------------------------------------------------------------------*/
70/*---------------------------------------------------------------------------*/
71
72Int32 ExceptionUtils::
73print(const std::exception& ex, ITraceMng* msg, bool is_no_continue)
74{
75 const char* nc = _noContinueString(is_no_continue);
76 if (msg) {
77 msg->error() << "** A standard exception occured: " << ex.what() << ".\n"
78 << nc;
79 }
80 else {
81 std::cerr << "** A standard exception occured: " << ex.what() << ".\n"
82 << nc;
83 }
84 return 2;
85}
86
87/*---------------------------------------------------------------------------*/
88/*---------------------------------------------------------------------------*/
89
90Int32 ExceptionUtils::
91print(const Exception& ex, ITraceMng* msg, bool is_no_continue)
92{
93 const char* nc = _noContinueString(is_no_continue);
94 if (msg) {
95 if (!ex.isCollective() || msg->isMaster())
96 msg->error() << ex << '\n'
97 << nc;
98 }
99 else {
100 std::cerr << "EXCEPTION: " << ex << '\n'
101 << nc;
102 }
103 return 3;
104}
105
106/*---------------------------------------------------------------------------*/
107/*---------------------------------------------------------------------------*/
108
109Int32 ExceptionUtils::
110callWithTryCatch(std::function<void()> function, ITraceMng* tm)
111{
112 try {
113 function();
114 }
115 catch (const Exception& ex) {
116 return print(ex, tm, false);
117 }
118 catch (const std::exception& ex) {
119 return print(ex, tm, false);
120 }
121 catch (...) {
122 return print(tm, false);
123 }
124 return 0;
125}
126
127/*---------------------------------------------------------------------------*/
128/*---------------------------------------------------------------------------*/
129
130void ExceptionUtils::
131callAndTerminateIfThrow(std::function<void()> function, ITraceMng* tm)
132{
133 int r = callWithTryCatch(function, tm);
134 if (r != 0) {
135 std::cerr << "Exception catched in arcaneCallFunctionAndTerminateIfThrow: calling std::terminate()\n";
136 std::terminate();
137 }
138}
139
140/*---------------------------------------------------------------------------*/
141/*---------------------------------------------------------------------------*/
142
143} // namespace Arcane
144
145/*---------------------------------------------------------------------------*/
146/*---------------------------------------------------------------------------*/
Utility functions for exception handling.
Int32 print(ITraceMng *tm, bool is_no_continue=true)
Prints a message for an unknown exception.
Int32 callWithTryCatch(std::function< void()> function, ITraceMng *tm=nullptr)
Calls a function while catching and displaying exceptions.
bool isCollective() const
True if it is a collective error (concerns all processors).
virtual TraceMessage error()=0
Stream for an error message.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
std::int32_t Int32
Signed integer type of 32 bits.