Flow123d  release_2.2.0-914-gf1a3a4f
exceptions.cc
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file exceptions.cc
15  * @brief
16  */
17 
18 #include "system.hh"
19 #include "exceptions.hh"
20 #include <iostream>
21 #include <cstring>
22 #include <boost/exception/diagnostic_information.hpp>
23 
24 
26  this->frames_to_cut_ = { "boost", "exception_detail", "throw_exception"};
27 }
28 
29 
30 
33 
34 
35 
36 
38 
39 
40 
41 
42 void ExceptionBase::print_stacktrace(std::ostream &out) const {
44 }
45 
46 
47 const char * ExceptionBase::what() const throw () {
48  // have preallocated some space for error message we want to return
49  // Is there any difference, if we move this into ExceptionBase ??
50  static std::string message(1024,' ');
51 
52  // Be sure that this function do not throw.
53  try {
54  std::ostringstream converter;
55  this->form_message(converter);
56  if (EI_NestedMessage::ptr(*this)) {
57  converter << "--------------------------------------------------------" << std::endl;
58  converter << "Nested exception:" << *(EI_NestedMessage::ptr(*this)) << std::endl;
59  }
60  if (EI_Nested::ptr(*this)) {
61  std::shared_ptr<ExceptionBase> exc_ptr = *EI_Nested::ptr(*this);
62  while (exc_ptr) {
63  converter << "--------------------------------------------------------" << std::endl;
64  converter << "Nested exception:" << std::endl;
65  exc_ptr->form_message(converter);
66  if (EI_NestedMessage::ptr(*exc_ptr)) {
67  converter << "--------------------------------------------------------" << std::endl;
68  converter << "Nested exception:" << *(EI_NestedMessage::ptr(*exc_ptr)) << std::endl;
69  exc_ptr.reset();
70  }
71  if (EI_Nested::ptr(*exc_ptr)) {
72  exc_ptr = *EI_Nested::ptr(*exc_ptr);
73  } else {
74  exc_ptr.reset();
75  }
76  }
77  }
78  message = converter.str();
79  return message.c_str();
80 
81  } catch (std::exception &exc) {
82  std::cerr << "*** Exception encountered in exception handling routines ***" << std::endl << "*** Message is " << std::endl
83  << exc.what() << std::endl << "*** Aborting! ***" << std::endl;
84 
85  std::abort();
86  } catch (...) {
87  std::cerr << "*** Exception encountered in exception handling routines ***" << std::endl << "*** Aborting! ***"
88  << std::endl;
89 
90  std::abort();
91  }
92  return 0;
93 }
94 
95 
96 std::string ExceptionBase::what_type_msg() const {
97  return "Program Error: ";
98 }
99 
100 
101 std::ostringstream &ExceptionBase::form_message(std::ostringstream &converter) const {
102 
103  converter << "--------------------------------------------------------" << std::endl;
104  converter << this->what_type_msg();
105  this->print_info(converter);
106 
107  converter << "\n** Diagnosting info **\n" ;
108  converter << boost::diagnostic_information_what( *this );
109  print_stacktrace(converter);
110  converter << std::endl << "--------------------------------------------------------" << std::endl;
111 
112  return converter;
113 }
114 
115 
116 
const char * what() const
Definition: exceptions.cc:47
ExceptionBase()
Default constructor, just calls fill_stacktrace().
Definition: exceptions.cc:25
void print(std::ostream &out, std::vector< std::string > frames_to_cut=std::vector< std::string >()) const
Prints formated stacktrace into given stream out.
Definition: stack_trace.cc:72
StackTrace stack_trace_
Stacktrace of exception.
Definition: exceptions.hh:107
std::vector< std::string > frames_to_cut_
Stacktrace frames, which will be cut, see StackTrace::print method.
Definition: exceptions.hh:109
virtual std::ostringstream & form_message(std::ostringstream &) const
Definition: exceptions.cc:101
virtual std::string what_type_msg() const
Return type of message ("Program error" for this class). Can be override in descendants.
Definition: exceptions.cc:96
virtual ~ExceptionBase()
Destructor, possibly free stacktrace.
Definition: exceptions.cc:37
Base of exceptions used in Flow123d.
Definition: exceptions.hh:75
void print_stacktrace(std::ostream &out) const
Prints formated stacktrace into given stream out.
Definition: exceptions.cc:42
virtual void print_info(std::ostringstream &out) const =0