From b19c03f52434af12be904c233a8dfbaece85d215 Mon Sep 17 00:00:00 2001 From: Christian Date: Fri, 1 Mar 2019 11:12:44 +0100 Subject: [PATCH] Added myexeption to throw errors and show using MyExeption class. --- src/mc_electron/bedionization.cpp | 7 ++++--- src/mc_electron/electron.cpp | 4 +++- src/mc_electron/functions.cpp | 17 +++++++++-------- src/mc_electron/ionization.cpp | 4 +++- src/mc_electron/katrin.cpp | 8 +++++--- src/mc_electron/logger.cpp | 4 +++- src/mc_electron/main.cpp | 11 ++++++++--- src/mc_electron/myexeption.h | 24 ++++++++++++++++++++++++ src/mc_electron/parameter.cpp | 16 ++++++++++------ src/mc_electron/simulation.cpp | 13 +++++++------ 10 files changed, 76 insertions(+), 32 deletions(-) create mode 100644 src/mc_electron/myexeption.h diff --git a/src/mc_electron/bedionization.cpp b/src/mc_electron/bedionization.cpp index 872590a..8e2848b 100644 --- a/src/mc_electron/bedionization.cpp +++ b/src/mc_electron/bedionization.cpp @@ -9,6 +9,7 @@ #include "bedionization.hpp" +#include "myexeption.h" #include "constants.hpp" #include @@ -27,7 +28,7 @@ double BED_Ionization::get_TCS(double T_eV) { if (T_eV<=0) { stringstream ss; ss<<"get_TCS(double T_eV): Input energy T_eV="<= (T_eV-B_eV) ) ) { diff --git a/src/mc_electron/electron.cpp b/src/mc_electron/electron.cpp index 0cca720..fecd5d9 100644 --- a/src/mc_electron/electron.cpp +++ b/src/mc_electron/electron.cpp @@ -8,6 +8,8 @@ #include "electron.hpp" +#include "myexeption.h" + using namespace std; @@ -52,6 +54,6 @@ int Electron::roll_Interaction(vec3 speedT2_ms, ranlib::Uniform *rngUni) } } - throw ("Interaction::roll_Interaction: Error in finding Interaction."); + throw MyException("Interaction::roll_Interaction: Error in finding Interaction."); } diff --git a/src/mc_electron/functions.cpp b/src/mc_electron/functions.cpp index b9219d7..2ee5455 100644 --- a/src/mc_electron/functions.cpp +++ b/src/mc_electron/functions.cpp @@ -9,6 +9,7 @@ #include "functions.hpp" +#include "myexeption.h" #include using namespace std; @@ -23,11 +24,11 @@ double calc_Ekin_eV_from_speed(double Speed_ms, double mass_eV) { if (beta>=1) { stringstream ss; ss<<"calc_Ekin_eV_from_speed(): Input speed v="<0."; - throw (ss.str()); + throw MyException(ss.str()); } double gamma = calc_gamma_from_beta(beta); return ( (gamma-1) * mass_eV ); @@ -37,11 +38,11 @@ double calc_gamma_from_beta(double beta) { if (beta>=1) { stringstream ss; ss<<"calc_gamma_from_beta(): Input beta ="< *rngUni, ranl Ekin_eV = roll_MBEnergy(MBTemp_K,me_eV, rngNormal); break; default: - throw ("Simulation::injectElectron: Invalid Injection Method."); + throw MyException("Simulation::injectElectron: Invalid Injection Method."); break; } vec3 speed_ms = roll_uniform_direction(rngUni) * calc_Speed_from_Ekin_eV(Ekin_eV,me_eV); @@ -180,7 +182,7 @@ bool Katrin::get_Terminated(Electron termElectron) { Ekin_eV = termElectron.getKinEnergy_eV(); ETemp_eV = kb_eVK*wgtsTemp_K; if (ETemp_eV<0) { - throw ("Simulation::checkTermination: ETemp_eV < 0."); + throw MyException("Simulation::checkTermination: ETemp_eV < 0."); } if (Ekin_eV<=cutoffEnergy_eV) { @@ -197,6 +199,6 @@ void Katrin::set_cutoffEnergy_eV() { } else { stringstream ss; ss<<"Mover::Mover(): cutoffMode= "< #include @@ -101,7 +103,7 @@ string Logger::getDirName() { if (mkdir(dirname.str().c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) { stringstream ss; ss<<"Simulation::initLogger: Could not create folder "< + +using namespace std; + +struct MyException : public exception +{ + string s; + MyException(string ss) : s(ss) {} + ~MyException() throw () {} // Updated + const char* what() const throw() { return s.c_str(); } +}; + +#endif /* myexeption_h */ diff --git a/src/mc_electron/parameter.cpp b/src/mc_electron/parameter.cpp index 3bcf071..fc5bdf8 100644 --- a/src/mc_electron/parameter.cpp +++ b/src/mc_electron/parameter.cpp @@ -9,6 +9,8 @@ #include "parameter.hpp" +#include "myexeption.h" + #include #include #include @@ -20,6 +22,7 @@ using namespace std; Parameter::Parameter() { // Simulation Specs + seed = (int)getValue("seed"); numTotalElectrons = (long int)getValue("numTotalElectrons"); dt_s = getValue("dt_s"); @@ -45,10 +48,11 @@ Parameter::Parameter() { // Output numberZBins = getValue ("numberZBins"); - } + +} -double Parameter::getValue(std::string varName) { +double Parameter::getValue(string varName) { char* ext = (char*)("cat"); char* str = strdup(varName.c_str()); double temp; @@ -58,13 +62,13 @@ double Parameter::getValue(std::string varName) { if (getenv("parpath") == NULL) { - throw ("Parameter::value: Environment variable parpath not defined."); + throw MyException("Parameter::value: Environment variable parpath not defined."); } strcpy(fname, getenv("parpath")); parfile = getenv("parfile"); if (parfile == NULL) { - throw ("Parameter::value: Environment variable parfile not defined."); + throw MyException("Parameter::value: Environment variable parfile not defined."); } if (!ext) { @@ -75,7 +79,7 @@ double Parameter::getValue(std::string varName) { { stringstream ss; ss<<"Parameter::value: Can not open file " < loc_m; while ( (numberElectrons < numTotalElectrons) || (electronQue.size() > 0) ) { if (!electronQue.empty()) { - throw ("Simulation::doTC6: electronQue should be empty"); + throw MyException("Simulation::doTC6: electronQue should be empty"); } electronQue.push_back(Kat.injectElectron(numberElectrons, &rngUni, &rngNormal)); @@ -69,7 +70,7 @@ void Simulation::doTC6() { } else { stringstream ss; ss<<"Simulation::doTC6(): propagationMethod= "<