From 2861486674c6fe23a3c17ab7653cb797670c0b10 Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Fri, 14 Aug 1998 14:23:53 +0000 Subject: [PATCH] Use C++ exception handling to guard in/out operations. Consistency and parameter check is and will still be done using the Assert macro. git-svn-id: https://svn.dealii.org/trunk@486 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/base/include/base/exceptions.h | 18 +++++++++++ deal.II/base/source/parameter_handler.cc | 16 +++++++++- deal.II/deal.II/Conventions | 8 ++++- deal.II/deal.II/Todo | 5 +++ deal.II/deal.II/include/grid/point.h | 8 +++++ .../deal.II/source/dofs/dof_constraints.cc | 3 ++ deal.II/deal.II/source/grid/tria.cc | 32 ++++++++++++++++++- deal.II/deal.II/source/numerics/data_io.cc | 22 +++++++++++-- deal.II/lac/source/Makefile | 2 +- deal.II/lac/source/dfmatrix.cc | 3 ++ deal.II/lac/source/dsmatrix.cc | 8 +++++ deal.II/lac/source/dvector.cc | 3 ++ 12 files changed, 121 insertions(+), 7 deletions(-) diff --git a/deal.II/base/include/base/exceptions.h b/deal.II/base/include/base/exceptions.h index 7599134993..f0161654c0 100644 --- a/deal.II/base/include/base/exceptions.h +++ b/deal.II/base/include/base/exceptions.h @@ -423,6 +423,24 @@ class Exception5 : public ExceptionBase { \ +/* ----------------------- Declare some global exceptions which are + not generated by the Assert mechanism, but + are rather used for the throw and catch + way. +*/ + +#include + +class GlobalExcIO : public exception { + public: + virtual const char * what () const { + return __PRETTY_FUNCTION__; + }; +}; + + + + /*---------------------------- exceptions.h ---------------------------*/ /* end of #ifndef __exceptions_H */ #endif diff --git a/deal.II/base/source/parameter_handler.cc b/deal.II/base/source/parameter_handler.cc index 2ac99bae69..8758a728b8 100644 --- a/deal.II/base/source/parameter_handler.cc +++ b/deal.II/base/source/parameter_handler.cc @@ -117,6 +117,9 @@ ParameterHandler::~ParameterHandler () {}; bool ParameterHandler::read_input (istream &input) { + if (!input) + throw GlobalExcIO (); + string line; int lineno=0; while (input) @@ -333,6 +336,9 @@ ostream & ParameterHandler::print_parameters (ostream &out, OutputStyle style) { // given as "style" Assert ((style == Text) || (style == LaTeX), ExcNotImplemented()); + if (!out) + throw GlobalExcIO (); + switch (style) { case Text: @@ -374,7 +380,10 @@ void ParameterHandler::print_parameters_section (ostream &out, // assert that only known formats are // given as "style" Assert ((style == Text) || (style == LaTeX), ExcNotImplemented()); - + + if (!out) + throw GlobalExcIO (); + Section *pd = get_present_defaults_subsection (); Section *pc = get_present_changed_subsection (); @@ -694,12 +703,16 @@ MultipleParameterLoop::~MultipleParameterLoop () {}; bool MultipleParameterLoop::read_input (istream &input) { + if (!input) + throw GlobalExcIO (); + bool x = ParameterHandler::read_input (input); if (x) init_branches (); return x; }; + bool MultipleParameterLoop::read_input (const string &filename) { // I don't know why it is necessary to // declare this function: simply not @@ -717,6 +730,7 @@ bool MultipleParameterLoop::read_input (const string &filename) { }; + bool MultipleParameterLoop::read_input_from_string (const char *s) { bool x = ParameterHandler::read_input (s); init_branches (); diff --git a/deal.II/deal.II/Conventions b/deal.II/deal.II/Conventions index eb6228ec65..5b38a2b2ee 100644 --- a/deal.II/deal.II/Conventions +++ b/deal.II/deal.II/Conventions @@ -55,5 +55,11 @@ Here are some conventions for source code of the deal.II library: input parameters shall precede the output parameters, unless there are good reasons to change this order. -11./ Each class has to have at least 200 pages of documentation ;-) +11./ Exceptions are used for internal parameter checking and for + consistency checks through the Assert macro. Exception handling + like done by the C++ language (try/throw/catch) are used to + handle run time errors (like I/O failures) which must be on + in any case, not only in debug mode. + +12./ Each class has to have at least 200 pages of documentation ;-) diff --git a/deal.II/deal.II/Todo b/deal.II/deal.II/Todo index 43a413357f..2a15993ef3 100644 --- a/deal.II/deal.II/Todo +++ b/deal.II/deal.II/Todo @@ -128,6 +128,11 @@ Remove the workaround with the BoundaryHelper class which was (previous ones worked flawless) +Check the pattern matching in the parameter handler module using + the C++ exception handling mechanism rather than Asserts, since + this should be on also when debug mode is off. + + diff --git a/deal.II/deal.II/include/grid/point.h b/deal.II/deal.II/include/grid/point.h index 709fbf9085..999877ae46 100644 --- a/deal.II/deal.II/include/grid/point.h +++ b/deal.II/deal.II/include/grid/point.h @@ -397,6 +397,10 @@ ostream & operator << (ostream &out, const Point &p) { for (unsigned int i=0; i inline ostream & operator << (ostream &out, const Point<1> &p) { out << p(0); + + if (!out) + throw GlobalExcIO (); + return out; }; diff --git a/deal.II/deal.II/source/dofs/dof_constraints.cc b/deal.II/deal.II/source/dofs/dof_constraints.cc index eeab1152af..aa91822b39 100644 --- a/deal.II/deal.II/source/dofs/dof_constraints.cc +++ b/deal.II/deal.II/source/dofs/dof_constraints.cc @@ -703,4 +703,7 @@ void ConstraintMatrix::print (ostream &out) const { out << " " << lines[i].line << " " << lines[i].entries[j].first << ": " << lines[i].entries[j].second << endl; + + if (!out) + throw GlobalExcIO (); }; diff --git a/deal.II/deal.II/source/grid/tria.cc b/deal.II/deal.II/source/grid/tria.cc index 57fe54ac1e..5b1675582a 100644 --- a/deal.II/deal.II/source/grid/tria.cc +++ b/deal.II/deal.II/source/grid/tria.cc @@ -1903,10 +1903,16 @@ void Triangulation::print_gnuplot (ostream &out, const unsigned int level) active_cell_iterator(end()) : begin_active (level+1)); + if (!out) + throw GlobalExcIO (); + out << "#Active cells on level " << level << ": " << n_active_cells (level) << endl; for (; cell != endc; ++cell) print_gnuplot (out, cell); + + if (!out) + throw GlobalExcIO (); }; @@ -1916,9 +1922,15 @@ void Triangulation::print_gnuplot (ostream &out, const unsigned int level) template <> void Triangulation<1>::print_gnuplot (ostream &out, const active_cell_iterator & cell) const { + if (!out) + throw GlobalExcIO (); + out << cell->vertex(0) << " " << cell->level() << endl << cell->vertex(1) << " " << cell->level() << endl << endl; + + if (!out) + throw GlobalExcIO (); }; #endif @@ -1929,6 +1941,9 @@ void Triangulation<1>::print_gnuplot (ostream &out, template <> void Triangulation<2>::print_gnuplot (ostream &out, const active_cell_iterator & cell) const { + if (!out) + throw GlobalExcIO (); + out << cell->vertex(0) << " " << cell->level() << endl << cell->vertex(1) << " " << cell->level() << endl << cell->vertex(2) << " " << cell->level() << endl @@ -1936,6 +1951,9 @@ void Triangulation<2>::print_gnuplot (ostream &out, << cell->vertex(0) << " " << cell->level() << endl << endl // double new line for gnuplot 3d plots << endl; + + if (!out) + throw GlobalExcIO (); }; #endif @@ -3512,6 +3530,9 @@ void Triangulation::write_bool_vector (const unsigned int magic_number1, for (unsigned int position=0; position::write_bool_vector (const unsigned int magic_number1, out << static_cast(flags[i]) << " "; out << endl << magic_number2 << endl; - + delete[] flags; + + if (!out) + throw GlobalExcIO (); }; @@ -3533,6 +3557,9 @@ void Triangulation::read_bool_vector (const unsigned int magic_number1, vector &v, const unsigned int magic_number2, istream &in) { + if (!in) + throw GlobalExcIO (); + unsigned int magic_number; in >> magic_number; Assert (magic_number==magic_number1, ExcGridReadError()); @@ -3556,6 +3583,9 @@ void Triangulation::read_bool_vector (const unsigned int magic_number1, Assert (magic_number==magic_number2, ExcGridReadError()); delete[] flags; + + if (!in) + throw GlobalExcIO (); }; diff --git a/deal.II/deal.II/source/numerics/data_io.cc b/deal.II/deal.II/source/numerics/data_io.cc index cbb1705890..4e43f7f324 100644 --- a/deal.II/deal.II/source/numerics/data_io.cc +++ b/deal.II/deal.II/source/numerics/data_io.cc @@ -51,6 +51,8 @@ void DataIn::read_ucd (istream &in) { Assert (tria != 0, ExcNoTriangulationSelected()); Assert ((1<=dim) && (dim<=2), ExcNotImplemented()); + if (!in) + throw GlobalExcIO (); // skip comments at start of file char c; @@ -170,7 +172,10 @@ void DataIn::read_ucd (istream &in) { // check that no forbidden arrays are used Assert (subcelldata.check_consistency(dim), ExcInternalError()); - + + if (!in) + throw GlobalExcIO (); + tria->create_triangulation (vertices, cells, subcelldata); }; @@ -237,7 +242,7 @@ void DataOut::write_ucd (ostream &out) const { DoFHandler::active_cell_iterator cell, endc = dofs->end(); - unsigned int n_vertex_dofs; + unsigned int n_vertex_dofs = 0; // first loop over all cells to // find out how many degrees of @@ -254,7 +259,6 @@ void DataOut::write_ucd (ostream &out) const { ++vertex) is_vertex_dof[cell->vertex_dof_index(vertex,0)] = true; - n_vertex_dofs = 0; for (unsigned i=0; i!=is_vertex_dof.size(); ++i) if (is_vertex_dof[i] == true) ++n_vertex_dofs; @@ -384,6 +388,9 @@ void DataOut::write_ucd (ostream &out) const { }; // no cell data // no model data + if (!out) + throw GlobalExcIO (); + }; @@ -453,6 +460,9 @@ void DataOut::write_ucd_faces (ostream &out, ++index; }; + + if (!out) + throw GlobalExcIO (); }; @@ -562,6 +572,9 @@ void DataOut::write_gnuplot (ostream &out) const { Assert (false, ExcNotImplemented()); }; }; + + if (!out) + throw GlobalExcIO (); }; @@ -655,6 +668,9 @@ void DataOut<2>::write_povray (ostream &out) const { << endl; }; out << "}"; + + if (!out) + throw GlobalExcIO (); }; diff --git a/deal.II/lac/source/Makefile b/deal.II/lac/source/Makefile index 04a727f52a..3efa428fb4 100644 --- a/deal.II/lac/source/Makefile +++ b/deal.II/lac/source/Makefile @@ -3,7 +3,7 @@ CXX = c++ INCLUDE = -I../include -I../../base/include CXXFLAGS.g= -DDEBUG -g -Wall -W -pedantic -Wconversion \ - -Winline -Woverloaded-virtual -fno-rtti -fno-exceptions \ + -Winline -Woverloaded-virtual -fno-rtti \ $(INCLUDE) CXXFLAGS =-O3 -Wuninitialized -finline-functions -ffast-math \ -funroll-loops -felide-constructors -fnonnull-objects \ diff --git a/deal.II/lac/source/dfmatrix.cc b/deal.II/lac/source/dfmatrix.cc index 2b95838794..34b785541a 100644 --- a/deal.II/lac/source/dfmatrix.cc +++ b/deal.II/lac/source/dfmatrix.cc @@ -1102,5 +1102,8 @@ void dFMatrix::print_formatted (ostream &out, const unsigned int precision) cons out << endl; }; + if (!out) + throw GlobalExcIO (); + out.setf (0, ios::floatfield); // reset output format }; diff --git a/deal.II/lac/source/dsmatrix.cc b/deal.II/lac/source/dsmatrix.cc index 51097ecf5e..d6dcce0e3d 100644 --- a/deal.II/lac/source/dsmatrix.cc +++ b/deal.II/lac/source/dsmatrix.cc @@ -327,6 +327,9 @@ dSMatrixStruct::print_gnuplot (ostream &out) const for (unsigned int j=rowstart[i]; j=0) out << i << " " << -colnums[j] << endl; + + if (!out) + throw GlobalExcIO (); } @@ -748,6 +751,9 @@ void dSMatrix::print (ostream &out) const { for (unsigned int i=0; irows; ++i) for (unsigned int j=cols->rowstart[i]; jrowstart[i+1]; ++j) out << "(" << i << "," << cols->colnums[j] << ") " << val[j] << endl; + + if (!out) + throw GlobalExcIO (); }; @@ -768,6 +774,8 @@ void dSMatrix::print_formatted (ostream &out, const unsigned int precision) cons out << setw(precision+8) << " "; out << endl; }; + if (!out) + throw GlobalExcIO (); out.setf (0, ios::floatfield); // reset output format }; diff --git a/deal.II/lac/source/dvector.cc b/deal.II/lac/source/dvector.cc index 9a40eea7fb..05febceda7 100644 --- a/deal.II/lac/source/dvector.cc +++ b/deal.II/lac/source/dvector.cc @@ -609,6 +609,9 @@ void dVector::print (ostream &out) const { Assert (dim!=0, ExcEmptyVector()); for (unsigned int i=0; i