Arcane  v4.1.1.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
ExceptionUtils.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2025 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/* Fonctions utilitaires pour la gestion des exceptions. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
15
16#include "arccore/base/Exception.h"
17#include "arccore/trace/ITraceMng.h"
18
19#include <iostream>
20
21/*---------------------------------------------------------------------------*/
22/*---------------------------------------------------------------------------*/
28/*---------------------------------------------------------------------------*/
29/*---------------------------------------------------------------------------*/
30
31namespace Arcane
32{
38/*---------------------------------------------------------------------------*/
39/*---------------------------------------------------------------------------*/
40
41namespace
42{
43 const char* _noContinueString(bool is_no_continue)
44 {
45 return (is_no_continue) ? "** Can't continue with the execution.\n" : "";
46 }
47} // namespace
48
49/*---------------------------------------------------------------------------*/
50/*---------------------------------------------------------------------------*/
51
52Int32 ExceptionUtils::
53print(ITraceMng* msg, bool is_no_continue)
54{
55 const char* nc = _noContinueString(is_no_continue);
56 const char* msg_str = "** An unknowed error occured...\n";
57 if (msg) {
58 msg->error() << msg_str << nc;
59 }
60 else {
61 std::cerr << msg_str << nc;
62 }
63 return 1;
64}
65
66/*---------------------------------------------------------------------------*/
67/*---------------------------------------------------------------------------*/
68
69Int32 ExceptionUtils::
70print(const std::exception& ex, ITraceMng* msg, bool is_no_continue)
71{
72 const char* nc = _noContinueString(is_no_continue);
73 if (msg) {
74 msg->error() << "** A standard exception occured: " << ex.what() << ".\n"
75 << nc;
76 }
77 else {
78 std::cerr << "** A standard exception occured: " << ex.what() << ".\n"
79 << nc;
80 }
81 return 2;
82}
83
84/*---------------------------------------------------------------------------*/
85/*---------------------------------------------------------------------------*/
86
87Int32 ExceptionUtils::
88print(const Exception& ex, ITraceMng* msg, bool is_no_continue)
89{
90 const char* nc = _noContinueString(is_no_continue);
91 if (msg) {
92 if (!ex.isCollective() || msg->isMaster())
93 msg->error() << ex << '\n'
94 << nc;
95 }
96 else {
97 std::cerr << "EXCEPTION: " << ex << '\n'
98 << nc;
99 }
100 return 3;
101}
102
103/*---------------------------------------------------------------------------*/
104/*---------------------------------------------------------------------------*/
105
106Int32 ExceptionUtils::
107callWithTryCatch(std::function<void()> function, ITraceMng* tm)
108{
109 try {
110 function();
111 }
112 catch (const Exception& ex) {
113 return print(ex, tm, false);
114 }
115 catch (const std::exception& ex) {
116 return print(ex, tm, false);
117 }
118 catch (...) {
119 return print(tm, false);
120 }
121 return 0;
122}
123
124/*---------------------------------------------------------------------------*/
125/*---------------------------------------------------------------------------*/
126
127void ExceptionUtils::
128callAndTerminateIfThrow(std::function<void()> function, ITraceMng* tm)
129{
130 int r = callWithTryCatch(function, tm);
131 if (r != 0) {
132 std::cerr << "Exception catched in arcaneCallFunctionAndTerminateIfThrow: calling std::terminate()\n";
133 std::terminate();
134 }
135}
136
137/*---------------------------------------------------------------------------*/
138/*---------------------------------------------------------------------------*/
139
140} // namespace Arcane
141
142/*---------------------------------------------------------------------------*/
143/*---------------------------------------------------------------------------*/
Fonctions utilitaires pour la gestion des exceptions.
ARCCORE_COMMON_EXPORT Int32 print(ITraceMng *tm, bool is_no_continue=true)
Imprime un message pour une exception inconnue.
ARCCORE_COMMON_EXPORT Int32 callWithTryCatch(std::function< void()> function, ITraceMng *tm=nullptr)
Appelle une fonction en récupérant et affichant les exceptions.
Classe de base d'une exception.
bool isCollective() const
Vrai s'il s'agit d'une erreur collective (concerne tous les processeurs)
Interface du gestionnaire de traces.
virtual TraceMessage error()=0
Flot pour un message d'erreur.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
std::int32_t Int32
Type entier signé sur 32 bits.