All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Exception.cpp
Go to the documentation of this file.
1 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2  Copyright (c) 2013 The plumed team
3  (see the PEOPLE file at the root of the distribution for a list of names)
4 
5  See http://www.plumed-code.org for more information.
6 
7  This file is part of plumed, version 2.0.
8 
9  plumed is free software: you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  plumed is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with plumed. If not, see <http://www.gnu.org/licenses/>.
21 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
22 #include "Exception.h"
23 
24 #if ! defined(__PLUMED_EXCEPTIONS)
25 #include <execinfo.h>
26 #endif
27 
28 #include <cstdio>
29 #include <cstdlib>
30 
31 using namespace std;
32 namespace PLMD{
33 
34 std::string Exception::format(const std::string&msg,const std::string&file,unsigned line,const std::string&function){
35  std::string message;
36  message="\n+++ Internal PLUMED error";
37  if(file.length()>0){
38  char cline[1000];
39  sprintf(cline,"%u",line);
40  message += "\n+++ file "+file+", line "+cline;
41  if(function.length()>0) message +=", function "+function;
42  }
43  if(msg.length()>0) message +="\n+++ message: "+msg;
44  return message;
45 }
46 
47 
48 Exception::Exception():
49  msg(format("","",0,""))
50 {
52 }
53 
54 Exception::Exception(const std::string&msg):
55  msg(format(msg,"",0,""))
56 {
58 }
59 
60 Exception::Exception(const std::string&msg,const std::string&file,unsigned line,const std::string&function):
61  msg(format(msg,file,line,function))
62 {
64 }
65 
67 #if ! defined(__PLUMED_EXCEPTIONS)
68  fprintf(stderr,"%s",what());
69  fprintf(stderr,"\n");
70 
71  void* callstack[128];
72  int i, frames = backtrace(callstack, 128);
73  char** strs = backtrace_symbols(callstack, frames);
74  for (i = 0; i < frames; ++i) {
75  fprintf(stderr,"%s\n", strs[i]);
76  }
77  free(strs);
78 
79  std::abort();
80 #endif
81 }
82 
83 }
84 
85 
Exception()
Without message.
Definition: Exception.cpp:48
void abortIfExceptionsAreDisabled()
Method which aborts in case exceptions are disabled.
Definition: Exception.cpp:66
STL namespace.
virtual const char * what() const
Returns the error message.
Definition: Exception.h:111