Arcane  v3.14.10.0
Documentation utilisateur
Chargement...
Recherche...
Aucune correspondance
arcane/src/arcane/utils/Exception.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2022 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/* Exception.cc (C) 2000-2022 */
9/* */
10/* Déclarations et définitions liées aux exceptions. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/utils/Exception.h"
15#include "arcane/utils/ITraceMng.h"
16
17#include <iostream>
18
19/*---------------------------------------------------------------------------*/
20/*---------------------------------------------------------------------------*/
21
22namespace Arcane
23{
24
25/*---------------------------------------------------------------------------*/
26/*---------------------------------------------------------------------------*/
27
28namespace
29{
30const char* _noContinueString(bool is_no_continue)
31{
32 return (is_no_continue) ? "** Can't continue with the execution.\n" : "";
33}
34}
35
36/*---------------------------------------------------------------------------*/
37/*---------------------------------------------------------------------------*/
38
39extern "C++" ARCANE_UTILS_EXPORT Integer
40arcanePrintAnyException(ITraceMng* msg,bool is_no_continue)
41{
42 const char* nc = _noContinueString(is_no_continue);
43 const char* msg_str = "** An unknowed error occured...\n";
44 if (msg){
45 msg->error() << msg_str << nc;
46 }
47 else{
48 std::cerr << msg_str << nc;
49 }
50 return 1;
51}
52
53/*---------------------------------------------------------------------------*/
54/*---------------------------------------------------------------------------*/
55
56extern "C++" ARCANE_UTILS_EXPORT Integer
57arcanePrintStdException(const std::exception& ex,ITraceMng* msg,bool is_no_continue)
58{
59 const char* nc = _noContinueString(is_no_continue);
60 if (msg){
61 msg->error() << "** A standard exception occured: " << ex.what() << ".\n" << nc;
62 }
63 else{
64 std::cerr << "** A standard exception occured: " << ex.what() << ".\n" << nc;
65 }
66 return 2;
67}
68
69/*---------------------------------------------------------------------------*/
70/*---------------------------------------------------------------------------*/
71
72extern "C++" ARCANE_UTILS_EXPORT Integer
73arcanePrintArcaneException(const Exception& ex,ITraceMng* msg,bool is_no_continue)
74{
75 const char* nc = _noContinueString(is_no_continue);
76 if (msg){
77 if (!ex.isCollective() || msg->isMaster())
78 msg->error() << ex << '\n' << nc;
79 }
80 else{
81 std::cerr << "EXCEPTION: " << ex << '\n' << nc;
82 }
83 return 3;
84}
85
86/*---------------------------------------------------------------------------*/
87/*---------------------------------------------------------------------------*/
88
89extern "C++" ARCANE_UTILS_EXPORT Integer
90arcaneCallFunctionAndCatchException(std::function<void()> function)
91{
92 try{
93 function();
94 }
95 catch(const Exception& ex){
96 return arcanePrintArcaneException(ex,nullptr,false);
97 }
98 catch(const std::exception& ex){
99 return arcanePrintStdException(ex,nullptr,false);
100 }
101 catch(...){
102 return arcanePrintAnyException(nullptr,false);
103 }
104 return 0;
105}
106
107/*---------------------------------------------------------------------------*/
108/*---------------------------------------------------------------------------*/
109
110extern "C++" ARCANE_UTILS_EXPORT void
111arcaneCallFunctionAndTerminateIfThrow(std::function<void()> function)
112{
113 int r = arcaneCallFunctionAndCatchException(function);
114 if (r!=0){
115 std::cerr << "Exception catched in arcaneCallFunctionAndTerminateIfThrow: calling std::terminate()\n";
116 std::terminate();
117 }
118}
119
120/*---------------------------------------------------------------------------*/
121/*---------------------------------------------------------------------------*/
122
123} // End namespace Arcane
124
125/*---------------------------------------------------------------------------*/
126/*---------------------------------------------------------------------------*/
Classe de base d'une exception.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
Integer arcaneCallFunctionAndCatchException(std::function< void()> function)
void arcaneCallFunctionAndTerminateIfThrow(std::function< void()> function)
Int32 Integer
Type représentant un entier.