Exception.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2011
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
00026 
00027 */
00028 
00029 #include <iostream>
00030 #include <petsc.h>
00031 
00032 #include "Exception.hpp"
00033 //#include "LogFile.hpp"
00034 
00035 Exception::Exception(const std::string& rMessage,
00036                      const std::string& rFilename, unsigned lineNumber)
00037 {
00038     SetMessage(rMessage, rFilename, lineNumber);
00044     // std::string log_file_message = "Exception occurred (although possibly handled), error message:\n" + message;
00045     // LOG(1, log_file_message);
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      * Check if we're running in parallel.
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
Generated on Thu Dec 22 13:00:05 2011 for Chaste by  doxygen 1.6.3