Commit 62636134 authored by Tomáš Oberhuber's avatar Tomáš Oberhuber Committed by Tomáš Oberhuber
Browse files

Added FileCloseError exception.

parent 0b81f7e7
Loading
Loading
Loading
Loading
+32 −0
Original line number Diff line number Diff line
/***************************************************************************
                          FileCloseError.h  -  description
                             -------------------
    begin                : Mar 5, 2019
    copyright            : (C) 2019 by Tomas Oberhuber et al.
    email                : tomas.oberhuber@fjfi.cvut.cz
 ***************************************************************************/

/* See Copyright Notice in tnl/Copyright */

// Implemented by: Tomas Oberhuber

#pragma once

#include <string>
#include <stdexcept>
#include <TNL/String.h>

namespace TNL {
namespace Exceptions {

class FileCloseError
   : public std::runtime_error
{
public:
   FileCloseError( const String& fileName )
   : std::runtime_error( "An error occurred when closing file " + fileName + "." )
   {}
};

} // namespace Exceptions
} // namespace TNL
+11 −6
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@
#include <TNL/Exceptions/FileSerializationError.h>
#include <TNL/Exceptions/FileDeserializationError.h>
#include <TNL/Exceptions/FileOpenError.h>
#include <TNL/Exceptions/FileCloseError.h>

namespace TNL {

@@ -55,14 +56,18 @@ inline bool File::open( const String& fileName, Mode mode )

inline bool File::close()
{
   if( file.is_open() ) {
   if( file.is_open() )
   {
      try
      {
         file.close();
      if( ! file.good() ) {
         std::cerr << "I was not able to close the file " << fileName << " properly!" << std::endl;
         return false;
      }
      catch(...)
      {
         throw Exceptions::FileCloseError( fileName );
      }
   }
   // reset all attributes
   // reset file name
   fileName = "";
   return true;
}