Skip to content
Snippets Groups Projects
Commit 927f93b7 authored by Giovanni La Mura's avatar Giovanni La Mura
Browse files

Create an error library to collect exceptions

parent 9a2b8adb
No related branches found
No related tags found
No related merge requests found
......@@ -8,6 +8,10 @@
#include <fstream>
#include <string>
#ifndef INCLUDE_ERRORS_H_
#include "../include/errors.h"
#endif
#ifndef INCLUDE_CONFIGURATION_H_
#include "../include/Configuration.h"
#endif
......
......@@ -28,52 +28,6 @@
#ifndef INCLUDE_CONFIGURATION_H_
#define INCLUDE_CONFIGURATION_H_
/**
* \brief Exception for open file error handlers.
*/
class OpenConfigurationFileException: public std::exception {
protected:
//! \brief Name of the file that was accessed.
std::string file_name;
public:
/**
* \brief Exception instance constructor.
*
* \param name: `string` Name of the file that was accessed.
*/
OpenConfigurationFileException(std::string name) { file_name = name; }
/**
* \brief Exception message.
*/
virtual const char* what() const throw() {
return file_name.c_str();
}
};
/**
* \brief Exception for unrecognized configuration data sets.
*/
class UnrecognizedConfigurationException: public std::exception {
protected:
//! Description of the problem.
std::string message;
public:
/**
* \brief Exception instance constructor.
*
* \param problem: `string` Description of the problem that occurred.
*/
UnrecognizedConfigurationException(std::string problem) { message = problem; }
/**
* \brief Exception message.
*/
virtual const char* what() const throw() {
return message.c_str();
}
};
/**
* \brief A class to represent the configuration of the scattering geometry.
*
......
......@@ -6,38 +6,7 @@
#ifndef INCLUDE_LIST_H_
#define INCLUDE_LIST_H_
/**
* \brief Exception for out of bounds List requests.
*/
class ListOutOfBoundsException: public std::exception {
protected:
//! Description of the problem.
std::string message;
public:
/**
* \brief Exception instance constructor.
*
* \param requested: `int` The index that was requested.
* \param min: `int` The minimum index allowed by the list.
* \param max: `int` The maximum index allowed by the list.
*/
ListOutOfBoundsException(int requested, int min, int max) {
message = "Error: requested index " + std::to_string(requested)
+ " out of list allowed bounds [" + std::to_string(min) + ", "
+ std::to_string(max - 1) + "]";
}
/**
* \brief Exception message.
*/
virtual const char* what() const throw() {
return message.c_str();
}
};
/**
* \brief A class to represent dynamic lists.
/*! \brief A class to represent dynamic lists.
*
* This class helps in the creation and management of dynamic lists of
* objects, whose size is not known in advance. List offers the advantage
......
......@@ -6,28 +6,6 @@
#ifndef INCLUDE_TRANSITIONMATRIX_H_
#define INCLUDE_TRANSITIONMATRIX_H_
/**
* \brief Exception for unrecognized file formats.
*/
class UnrecognizedFormatException: public std::exception {
protected:
//! Description of the problem.
std::string message;
public:
/**
* \brief Exception instance constructor.
*
* \param problem: `string` Description of the problem that occurred.
*/
UnrecognizedFormatException(std::string problem) { message = problem; }
/**
* \brief Exception message.
*/
virtual const char* what() const throw() {
return message.c_str();
}
};
/*! \brief Class to represent the Transition Matrix.
*/
class TransitionMatrix {
......
/*! \file errors.h
*
* \brief Collection of proprietary code exceptions.
*
* There are many circumstances that can prevent the correct execution
* of a code. These range from user mistakes, to improper configuration,
* to unsupported hardware and all the way up to various system failures.
* Although it is not possible to grant proper execution in all cases,
* it is often possible to design a code in such a way that the program
* detects unexpected conditions, informs the user and takes the proper
* actions, eventually stopping without crash, if no other options are
* available. C++ handles such unexpected circumstances by means of
* `exceptions`. These are special procedures that can be launched
* whenever an unexpected situation occurs and they allow to restore the
* code work-flow and attempt recovery. Exceptions can be divided in
* different cathegories, which respond to various types of problems.
* This library contains a set of exceptions designed to the most common
* problems that may occur while executing an application of the `NP_TMcode`
* suite.
*/
#ifndef INCLUDE_ERRORS_H_
#define INCLUDE_ERRORS_H_
/*! \brief Exception for out of bounds List requests.
*/
class ListOutOfBoundsException: public std::exception {
protected:
//! Description of the problem.
std::string message;
public:
/**
* \brief Exception instance constructor.
*
* \param requested: `int` The index that was requested.
* \param min: `int` The minimum index allowed by the list.
* \param max: `int` The maximum index allowed by the list.
*/
ListOutOfBoundsException(int requested, int min, int max) {
message = "Error: requested index " + std::to_string(requested)
+ " out of list allowed bounds [" + std::to_string(min) + ", "
+ std::to_string(max - 1) + "]";
}
/**
* \brief Exception message.
*/
virtual const char* what() const throw() {
return message.c_str();
}
};
/*! \brief Exception for open file error handlers.
*/
class OpenConfigurationFileException: public std::exception {
protected:
//! \brief Name of the file that was accessed.
std::string file_name;
public:
/**
* \brief Exception instance constructor.
*
* \param name: `string` Name of the file that was accessed.
*/
OpenConfigurationFileException(std::string name) { file_name = name; }
/**
* \brief Exception message.
*/
virtual const char* what() const throw() {
return file_name.c_str();
}
};
/*! \brief Exception for access requests out of matrix bounds.
*/
class MatrixOutOfBoundsException: public std::exception {
protected:
//! Description of the problem.
std::string message;
public:
/**
* \brief Exception instance constructor.
*
* \param problem: `string` Description of the problem that occurred.
*/
MatrixOutOfBoundsException(std::string problem) { message = problem; }
/**
* \brief Exception message.
*/
virtual const char* what() const throw() {
return message.c_str();
}
};
/*! \brief Exception for unrecognized configuration data sets.
*/
class UnrecognizedConfigurationException: public std::exception {
protected:
//! Description of the problem.
std::string message;
public:
/**
* \brief Exception instance constructor.
*
* \param problem: `string` Description of the problem that occurred.
*/
UnrecognizedConfigurationException(std::string problem) { message = problem; }
/**
* \brief Exception message.
*/
virtual const char* what() const throw() {
return message.c_str();
}
};
/*! \brief Exception for unrecognized file formats.
*/
class UnrecognizedFormatException: public std::exception {
protected:
//! Description of the problem.
std::string message;
public:
/**
* \brief Exception instance constructor.
*
* \param problem: `string` Description of the problem that occurred.
*/
UnrecognizedFormatException(std::string problem) { message = problem; }
/**
* \brief Exception message.
*/
virtual const char* what() const throw() {
return message.c_str();
}
};
/*! \brief Exception for unrecognized parameters.
*/
class UnrecognizedParameterException: public std::exception {
protected:
//! Description of the problem.
std::string message;
public:
/**
* \brief Exception instance constructor.
*
* \param problem: `string` Description of the problem that occurred.
*/
UnrecognizedParameterException(std::string problem) { message = problem; }
/**
* \brief Exception message.
*/
virtual const char* what() const throw() {
return message.c_str();
}
};
#endif
......@@ -6,50 +6,6 @@
#ifndef INCLUDE_TFRFME_H_
#define INCLUDE_TFRFME_H_
/**
* \brief Exception for unrecognized parameters.
*/
class MatrixOutOfBoundsException: public std::exception {
protected:
//! Description of the problem.
std::string message;
public:
/**
* \brief Exception instance constructor.
*
* \param problem: `string` Description of the problem that occurred.
*/
MatrixOutOfBoundsException(std::string problem) { message = problem; }
/**
* \brief Exception message.
*/
virtual const char* what() const throw() {
return message.c_str();
}
};
/**
* \brief Exception for unrecognized parameters.
*/
class UnrecognizedParameterException: public std::exception {
protected:
//! Description of the problem.
std::string message;
public:
/**
* \brief Exception instance constructor.
*
* \param problem: `string` Description of the problem that occurred.
*/
UnrecognizedParameterException(std::string problem) { message = problem; }
/**
* \brief Exception message.
*/
virtual const char* what() const throw() {
return message.c_str();
}
};
/*! \brief Class to represent the trapping configuration.
*/
class TFRFME {
......
......@@ -12,6 +12,10 @@
#include <regex>
#include <string>
#ifndef INCLUDE_ERRORS_H_
#include "../include/errors.h"
#endif
#ifndef INCLUDE_LIST_H_
#include "../include/List.h"
#endif
......
......@@ -3,10 +3,21 @@
* \brief Implementation of the parsing functions.
*/
#include <exception>
#include <fstream>
#include <string>
#ifndef INCLUDE_ERRORS_H_
#include "../include/errors.h"
#endif
#ifndef INCLUDE_LIST_H_
#include "../include/List.h"
#endif
#ifndef INCLUDE_PARSERS_H_
#include "../include/Parsers.h"
#endif
std::string *load_file(std::string file_name, int *count = 0) {
std::fstream input_file(file_name.c_str(), std::ios::in);
......
......@@ -8,6 +8,10 @@
#include <hdf5.h>
#include <string>
#ifndef INCLUDE_ERRORS_H_
#include "../include/errors.h"
#endif
#ifndef INCLUDE_LIST_H_
#include "../include/List.h"
#endif
......
......@@ -2,11 +2,15 @@
*
* \brief Implementation of file I/O operations.
*/
#include <stdexcept>
#include <exception>
#include <regex>
#include <string>
#include <hdf5.h>
#ifndef INCLUDE_ERRORS_H_
#include "../include/errors.h"
#endif
#ifndef INCLUDE_LIST_H_
#include "../include/List.h"
#endif
......@@ -126,7 +130,7 @@ HDFFile* HDFFile::from_schema(
delete[] max_dims;
} else {
string message = "unrecognized type \"" + rec_types[ri] + "\"\n";
throw runtime_error(message);
throw UnrecognizedParameterException(message);
}
}
......@@ -159,7 +163,7 @@ herr_t HDFFile::read(
case 2:
mem_type_id = H5T_NATIVE_DOUBLE; break;
default:
throw runtime_error("Unrecognized data type \"" + data_type + "\"");
throw UnrecognizedParameterException("unrecognized data type \"" + data_type + "\"");
}
if (dataset_id != H5I_INVALID_HID) {
status = H5Dread(dataset_id, mem_type_id, mem_space_id, file_space_id, dxpl_id, buffer);
......@@ -169,7 +173,7 @@ herr_t HDFFile::read(
status = (herr_t)-1;
}
} else {
throw runtime_error("Unrecognized data type \"" + data_type + "\"");
throw UnrecognizedParameterException("unrecognized data type \"" + data_type + "\"");
}
return status;
}
......@@ -198,7 +202,7 @@ herr_t HDFFile::write(
case 2:
mem_type_id = H5T_NATIVE_DOUBLE; break;
default:
throw runtime_error("Unrecognized data type \"" + data_type + "\"");
throw UnrecognizedParameterException("unrecognized data type \"" + data_type + "\"");
}
if (dataset_id != H5I_INVALID_HID) {
status = H5Dwrite(dataset_id, mem_type_id, mem_space_id, file_space_id, dxpl_id, buffer);
......@@ -208,7 +212,7 @@ herr_t HDFFile::write(
status = (herr_t)-1;
}
} else {
throw runtime_error("Unrecognized data type \"" + data_type + "\"");
throw UnrecognizedParameterException("unrecognized data type \"" + data_type + "\"");
}
return status;
}
......@@ -9,6 +9,10 @@
#include <hdf5.h>
#include <string>
#ifndef INCLUDE_ERRORS_H_
#include "../include/errors.h"
#endif
#ifndef INCLUDE_LIST_H_
#include "../include/List.h"
#endif
......
......@@ -8,6 +8,10 @@
#include <fstream>
#include <string>
#ifndef INCLUDE_ERRORS_H_
#include "../include/errors.h"
#endif
#ifndef INCLUDE_CONFIGURATION_H_
#include "../include/Configuration.h"
#endif
......
......@@ -2,11 +2,25 @@
#include <complex>
#include <cstdio>
#include <exception>
#include <hdf5.h>
#include <string>
#ifndef INCLUDE_ERRORS_H_
#include "../include/errors.h"
#endif
#ifndef INCLUDE_LIST_H_
#include "../include/List.h"
#endif
#ifndef INCLUDE_FILE_IO_H_
#include "../include/file_io.h"
#endif
#ifndef INCLUDE_CONFIGURATION_H_
#include "../include/Configuration.h"
#endif
using namespace std;
......
......@@ -2,11 +2,25 @@
#include <complex>
#include <cstdio>
#include <exception>
#include <hdf5.h>
#include <string>
#ifndef INCLUDE_ERRORS_H_
#include "../include/errors.h"
#endif
#ifndef INCLUDE_LIST_H_
#include "../include/List.h"
#endif
#ifndef INCLUDE_FILE_IO_H_
#include "../include/file_io.h"
#endif
#ifndef INCLUDE_TRANSITIONMATRIX_H_
#include "../include/TransitionMatrix.h"
#endif
using namespace std;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment