HermesCommon 1.0
exceptions.cpp
00001 // This file is part of HermesCommon
00002 //
00003 // Copyright (c) 2009 hp-FEM group at the University of Nevada, Reno (UNR).
00004 // Email: hpfem-group@unr.edu, home page: http:// hpfem.org/.
00005 //
00006 // Hermes2D is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published
00008 // by the Free Software Foundation; either version 2 of the License,
00009 // or (at your option) any later version.
00010 //
00011 // Hermes2D is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU General Public License
00017 // along with Hermes2D; if not, write to the Free Software
00018 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00019 
00020 #include "exceptions.h"
00021 #include <string>
00022 #include "api.h"
00023 #include "callstack.h"
00024 
00025 namespace Hermes
00026 {
00027   namespace Exceptions
00028   {
00029     Exception::Exception() : std::exception(), message(new char[1000])
00030     {
00031     }
00032 
00033     Exception::Exception(const char * msg, ...) : std::exception(), message(new char[1000])
00034     {
00035       char text[1024];
00036 
00037       // print the message
00038       va_list arglist;
00039       va_start(arglist, msg);
00040       vsprintf(text, msg, arglist);
00041       va_end(arglist);
00042 
00043       strcpy(message, text);
00044     }
00045 
00046     void Exception::print_msg() const
00047     {
00048       if(message)
00049         printf("Exception: %s\n", message);
00050       else
00051         printf("Default exception\n");
00052       if(Hermes::HermesCommonApi.get_integral_param_value(Hermes::exceptionsPrintCallstack) == 1)
00053         CallStack::dump(0);
00054     }
00055 
00056     Exception* Exception::clone()
00057     {
00058       return new Exception(*this);
00059     }
00060 
00061     const char * Exception::what() const throw()
00062     {
00063       char* messageWithReturn = new char[strlen(message)+2];
00064       strcpy(messageWithReturn, message);
00065       sprintf(messageWithReturn + strlen(message), "\n");
00066       return messageWithReturn;
00067     }
00068 
00069     NullException::NullException(int param_idx) : Exception()
00070     {
00071       this->param_idx = param_idx;
00072       this->item_idx = -1;
00073       char * msg = new char[27];
00074       sprintf(msg, "Parameter number %d is NULL", param_idx);
00075       message = msg;
00076     }
00077 
00078     NullException::NullException(int param_idx, int item_idx) : Exception()
00079     {
00080       this->param_idx = param_idx;
00081       this->item_idx = item_idx;
00082       char * msg = new char[55];
00083       sprintf(msg, "Element number %d of parameter number %d is NULL", item_idx, param_idx);
00084       message = msg;
00085     }
00086 
00087     int NullException::get_param_idx() const
00088     {
00089       return param_idx;
00090     }
00091 
00092     int NullException::get_item_idx() const
00093     {
00094       return item_idx;
00095     }
00096 
00097     NullException::NullException(const NullException & e)
00098     {
00099       char * msg = new char[strlen(e.what())+1];
00100       strcpy(msg, e.what());
00101       message = msg;
00102       param_idx = e.get_param_idx();
00103       item_idx = e.get_item_idx();
00104     }
00105 
00106     Exception* NullException::clone()
00107     {
00108       return new NullException(*this);
00109     }
00110 
00111     LengthException::LengthException(int param_idx, int wrong, int right) : Exception()
00112     {
00113       fst_param_idx = param_idx;
00114       this->wrong = wrong;
00115       this->right = right;
00116       this->snd_param_idx = -1;
00117       char * msg = new char[60];
00118       sprintf(msg, "Parameter number %d have length %d and should have %d", fst_param_idx, wrong, right);
00119       message = msg;
00120     }
00121 
00122     LengthException::LengthException(int fst_param_idx, int snd_param_idx, int first, int second) : Exception()
00123     {
00124       this->fst_param_idx = fst_param_idx;
00125       this->snd_param_idx = snd_param_idx;
00126       this->wrong = first;
00127       this->right = second;
00128       char * msg = new char[110];
00129       sprintf(msg, "Parameter number %d have length %d and parameter number %d have length %d. The lengths should be same",
00130             fst_param_idx, wrong, snd_param_idx, right);
00131       message = msg;
00132     }
00133 
00134     int LengthException::get_first_param_idx() const
00135     {
00136       return fst_param_idx;
00137     }
00138 
00139     int LengthException::get_second_param_idx() const
00140     {
00141       return snd_param_idx;
00142     }
00143 
00144     int LengthException::get_first_length() const
00145     {
00146       return wrong;
00147     }
00148 
00149     int LengthException::get_expected_length() const
00150     {
00151       return right;
00152     }
00153 
00154     LengthException::LengthException(const LengthException&e) : Exception()
00155     {
00156       char * msg = new char[strlen(e.what())+1];
00157       strcpy(msg, e.what());
00158       message = msg;
00159       this->fst_param_idx = e.get_first_param_idx();
00160       this->snd_param_idx = e.get_second_param_idx();
00161       this->wrong = e.get_first_length();
00162       this->right = e.get_expected_length();
00163     }
00164 
00165     Exception* LengthException::clone()
00166     {
00167       return new LengthException(*this);
00168     }
00169 
00170     LinearMatrixSolverException::LinearMatrixSolverException() : Exception()
00171     {
00172       char * msg =  new char[22];
00173       sprintf(msg, "Linear solver failed.");
00174       message = msg;
00175     }
00176 
00177     LinearMatrixSolverException::LinearMatrixSolverException(const char * reason) : Exception()
00178     {
00179       char * msg =  new char[34 + strlen(reason)];
00180       sprintf(msg, "Linear solver failed because:\"%s\"", reason);
00181       message = msg;
00182     }
00183 
00184     LinearMatrixSolverException::LinearMatrixSolverException(const LinearMatrixSolverException&e) : Exception()
00185     {
00186       char * msg = new char[strlen(e.what())+1];
00187       strcpy(msg, e.what());
00188       message = msg;
00189     }
00190 
00191     Exception* LinearMatrixSolverException::clone()
00192     {
00193       return new LinearMatrixSolverException(*this);
00194     }
00195 
00196     ValueException::ValueException(const char * name, double value, double allowed) : Exception()
00197     {
00198       char * msg =  new char[55 + strlen(name)];
00199       if(value>allowed)
00200         sprintf(msg, "Variable %s is %f but maximum allowed value is %f", name, value, allowed);
00201       else
00202         sprintf(msg, "Variable %s is %f but minimum allowed value is %f", name, value, allowed);
00203       message = msg;
00204       this->value = value;
00205       this->allowed = allowed;
00206     }
00207 
00208     ValueException::ValueException(const char * name, double value, double min, double max) : Exception()
00209     {
00210       char * msg = new char[70+strlen(name)];
00211       sprintf(msg, "Variable %s is %f allowed range is %f -- %f", name, value, min, max);
00212       message = msg;
00213       this->value = value;
00214       if(value>min)
00215         this->allowed = max;
00216       else
00217         this->allowed = min;
00218     }
00219 
00220     ValueException::ValueException(const char * name, std::string passed) : Exception()
00221     {
00222       char * msg = new char[70+strlen(name)];
00223       sprintf(msg, "Variable %s does not support value %s.", name, passed.c_str());
00224       message = msg;
00225     }
00226 
00227     double ValueException::get_value() const
00228     {
00229       return value;
00230     }
00231 
00232     double ValueException::get_allowed() const
00233     {
00234       return allowed;
00235     }
00236 
00237     ValueException::ValueException(const ValueException&e)
00238     {
00239       char * msg = new char[strlen(e.what())+1];
00240       strcpy(msg, e.what());
00241       message = msg;
00242       this->value = e.get_value();
00243       this->allowed = e.get_allowed();
00244     }
00245 
00246     Exception* ValueException::clone()
00247     {
00248       return new ValueException(*this);
00249     }
00250 
00251     MethodNotOverridenException::MethodNotOverridenException(const char * name, ...) : Exception()
00252     {
00253       char* text = new char[1024];
00254       sprintf(text, "Method not overriden: ");
00255 
00256       // print the message
00257       va_list arglist;
00258       va_start(arglist, name);
00259       vsprintf(text = text + strlen("Method not overriden: "), name, arglist);
00260       va_end(arglist);
00261       message = text - strlen("Method not overriden: ");
00262     }
00263 
00264     MethodNotOverridenException::MethodNotOverridenException(const MethodNotOverridenException&e)
00265     {
00266       char * msg = new char[strlen(e.what())+1];
00267       strcpy(msg, e.what());
00268       message = msg;
00269     }
00270 
00271     Exception* MethodNotOverridenException::clone()
00272     {
00273       return new MethodNotOverridenException(*this);
00274     }
00275 
00276     MeshLoadFailureException::MeshLoadFailureException(const char * reason, ...) : Exception()
00277     {
00278       char * text = new char[strlen(reason)+1];
00279 
00280       // print the message
00281       va_list arglist;
00282       va_start(arglist, reason);
00283       vsprintf(text, reason, arglist);
00284       va_end(arglist);
00285 
00286       message = text;
00287     }
00288 
00289     MeshLoadFailureException::MeshLoadFailureException(const MeshLoadFailureException&e)
00290     {
00291       char * msg = new char[strlen(e.what())+1];
00292       strcpy(msg, e.what());
00293       message = msg;
00294     }
00295 
00296     Exception* MeshLoadFailureException::clone()
00297     {
00298       return new MeshLoadFailureException(*this);
00299     }
00300 
00301     SpaceLoadFailureException::SpaceLoadFailureException(const char * reason, ...) : Exception()
00302     {
00303       char * text = new char[strlen(reason)+1];
00304 
00305       // print the message
00306       va_list arglist;
00307       va_start(arglist, reason);
00308       vsprintf(text, reason, arglist);
00309       va_end(arglist);
00310 
00311       message = text;
00312     }
00313 
00314     SpaceLoadFailureException::SpaceLoadFailureException(const SpaceLoadFailureException&e)
00315     {
00316       char * msg = new char[strlen(e.what())+1];
00317       strcpy(msg, e.what());
00318       message = msg;
00319     }
00320 
00321     Exception* SpaceLoadFailureException::clone()
00322     {
00323       return new SpaceLoadFailureException(*this);
00324     }
00325 
00326     SolutionSaveFailureException::SolutionSaveFailureException(const char * reason, ...) : Exception()
00327     {
00328       char * text = new char[strlen(reason)+1];
00329 
00330       // print the message
00331       va_list arglist;
00332       va_start(arglist, reason);
00333       vsprintf(text, reason, arglist);
00334       va_end(arglist);
00335 
00336       message = text;
00337     }
00338 
00339     SolutionSaveFailureException::SolutionSaveFailureException(const SolutionSaveFailureException&e)
00340     {
00341       char * msg = new char[strlen(e.what())+1];
00342       strcpy(msg, e.what());
00343       message = msg;
00344     }
00345 
00346     Exception* SolutionSaveFailureException::clone()
00347     {
00348       return new SolutionSaveFailureException(*this);
00349     }
00350 
00351     SolutionLoadFailureException::SolutionLoadFailureException(const char * reason, ...) : Exception()
00352     {
00353       char * text = new char[strlen(reason)+1];
00354 
00355       // print the message
00356       va_list arglist;
00357       va_start(arglist, reason);
00358       vsprintf(text, reason, arglist);
00359       va_end(arglist);
00360 
00361       message = text;
00362     }
00363 
00364     SolutionLoadFailureException::SolutionLoadFailureException(const SolutionLoadFailureException&e)
00365     {
00366       char * msg = new char[strlen(e.what())+1];
00367       strcpy(msg, e.what());
00368       message = msg;
00369     }
00370     
00371     Exception* SolutionLoadFailureException::clone()
00372     {
00373       return new SolutionLoadFailureException(*this);
00374     }
00375   }
00376 }