charon-core  0.3.1
ExceptionHandler.cpp
Go to the documentation of this file.
1 /* This file is part of Charon.
2 
3  Charon is free software: you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as published by
5  the Free Software Foundation, either version 3 of the License, or
6  (at your option) any later version.
7 
8  Charon is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU Lesser General Public License for more details.
12 
13  You should have received a copy of the GNU Lesser General Public License
14  along with Charon. If not, see <http://www.gnu.org/licenses/>.
15 */
22 #include <iostream>
23 #include <stdexcept>
24 #include <cstdlib>
25 #ifdef __GNUG__
26 #include <cxxabi.h>
27 #endif // __GNUG__
28 #include "../include/charon-core/ExceptionHandler.h"
29 #include "../include/charon-core/TypeDetector.h"
30 #include "../include/charon-core/SplitStream.h"
31 
32 int ExceptionHandler::run(int (&method)()) {
33  int ret = EXIT_FAILURE;
34  try {
35  ret = method();
36  }
37  catch (const std::exception& e) {
38  const char* name = typeid(e).name();
39 #ifdef __GNUG__
40  name = abi::__cxa_demangle(name, 0, 0, 0);
41 #endif // __GNUG__
42  std::cerr << "\n(EE) Caught exception of type " << name << ".\n";
43  std::cerr << "(EE) Message:\n(EE) \t" << e.what() << std::endl;
44  }
45  catch (const std::string& e) {
46  std::cerr << "\n(EE) Caught exception of type std::string.\n";
47  std::cerr << "(EE) Message:\n(EE) \t" << e << std::endl;
48  }
49  catch (const char*& e) {
50  std::cerr << "\n(EE) Caught exception of type const char*.\n";
51  std::cerr << "(EE) Message:\n(EE) \t" << e << std::endl;
52  }
53  catch (...) {
54  std::cerr << "\n(EE) Caught unknown exception." << std::endl;
55  }
56  return ret;
57 }
58 
59 int ExceptionHandler::run(void (&method)()) {
60  bool excpt = false;
61  try {
62  method();
63  }
64  catch (const std::exception& e) {
65  const char* name = typeid(e).name();
66 #ifdef __GNUG__
67  name = abi::__cxa_demangle(name, 0, 0, 0);
68 #endif // __GNUG__
69  std::cerr << "\n(EE) Caught exception of type " << name << ".\n";
70  std::cerr << "(EE) Message:\n(EE) \t" << e.what() << std::endl;
71  excpt = true;
72  }
73  catch (const std::string& e) {
74  std::cerr << "\n(EE) Caught exception of type std::string.\n";
75  std::cerr << "(EE) Message:\n(EE) \t" << e << std::endl;
76  excpt = true;
77  }
78  catch (const char*& e) {
79  std::cerr << "\n(EE) Caught exception of type const char*.\n";
80  std::cerr << "(EE) Message:\n(EE) \t" << e << std::endl;
81  excpt = true;
82  }
83  catch (...) {
84  std::cerr << "\n(EE) Caught unknown exception." << std::endl;
85  excpt = true;
86  }
87  return excpt ? EXIT_FAILURE : EXIT_SUCCESS;
88 }
89 
90 int ExceptionHandler::checkRaise(void (&method)()) {
91  bool excpt = false;
92  try {
93  method();
94  }
95  catch (const std::exception& e) {
96  const char* name = typeid(e).name();
97 #ifdef __GNUG__
98  name = abi::__cxa_demangle(name, 0, 0, 0);
99 #endif // __GNUG__
100  sout << "(II) Caught exception of type " << name << ".\n";
101  sout << "(II) Message:\n(II) \t" << e.what() << std::endl;
102  excpt = true;
103  }
104  catch (const std::string& e) {
105  sout << "(II) Caught exception of type std::string.\n";
106  sout << "(II) Message:\n(II) \t" << e << std::endl;
107  excpt = true;
108  }
109  catch (const char*& e) {
110  sout << "(II) Caught exception of type const char*.\n";
111  sout << "(II) Message:\n(II) \t" << e << std::endl;
112  excpt = true;
113  }
114  catch (...) {
115  sout << "(II) Caught unknown exception." << std::endl;
116  excpt = true;
117  }
118  if (excpt) {
119  return EXIT_SUCCESS;
120  }
121  sout << "\n(EE) Expected runtime error exception!" << std::endl;
122  return EXIT_FAILURE;
123 }
124 
SplitStream sout
Dummy instance for usage in other files (for interface too).
charon_plugins_DLL_PUBLIC int checkRaise(void(&method)())
execute given function and return EXIT_SUCCESS on exception catch
charon_plugins_DLL_PUBLIC int run(int(&method)())
Execute given function and catch exceptions.