Exception.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <iostream>
00030 #include <petsc.h>
00031
00032 #include "Exception.hpp"
00033
00034
00035 Exception::Exception(const std::string& rMessage,
00036 const std::string& rFilename, unsigned lineNumber)
00037 {
00038 SetMessage(rMessage, rFilename, lineNumber);
00044
00045
00046 }
00047
00048 void Exception::SetMessage(const std::string& rMessage,
00049 const std::string& rFilename, unsigned lineNumber)
00050 {
00051 mShortMessage = rMessage;
00052 std::stringstream line_number_stream;
00053 line_number_stream << lineNumber;
00054 mMessage = std::string("\nChaste error: ") + rFilename + ":" + line_number_stream.str() + ": " + mShortMessage;
00055 }
00056
00057 std::string Exception::GetMessage() const
00058 {
00059 return mMessage;
00060 }
00061
00062 std::string Exception::GetShortMessage() const
00063 {
00064 return mShortMessage;
00065 }
00066
00067 std::string Exception::CheckShortMessage(std::string expected) const
00068 {
00069 std::string error;
00070 if (mShortMessage != expected && mShortMessage != "Another process threw an exception; bailing out.")
00071 {
00072 error = "Incorrect exception message thrown: expected (" + expected + "); got (" + mShortMessage + ").";
00073 }
00074 return error;
00075 }
00076
00077 std::string Exception::CheckShortMessageContains(std::string expected) const
00078 {
00079 std::string error;
00080 if (mShortMessage.find(expected) == std::string::npos && mShortMessage != "Another process threw an exception; bailing out.")
00081 {
00082 error = "Incorrect exception message thrown: expected it to contain (" + expected + "); got (" + mShortMessage + ").";
00083 }
00084 return error;
00085 }
00086
00087 #define COVERAGE_IGNORE //Termination NEVER EVER happens under normal testing conditions.
00088 void Exception::Terminate(const std::string& rMessage, const std::string& rFilename, unsigned lineNumber)
00089 {
00090 std::stringstream error_message;
00091
00092 error_message << "\nChaste termination: " << rFilename << ":" << lineNumber << ": " << rMessage<<"\n";
00093 std::cerr << error_message.str();
00094
00095
00096
00097
00098 PetscTruth is_there;
00099 PetscInitialized(&is_there);
00100 if (is_there)
00101 {
00102 MPI_Abort(PETSC_COMM_WORLD, EXIT_FAILURE);
00103 }
00104 else
00105 {
00106 exit(EXIT_FAILURE);
00107 }
00108 }
00109
00110 #undef COVERAGE_IGNORE // Termination NEVER EVER happens under normal testing conditions