Skip to content
Snippets Groups Projects
Commit 62636134 authored by Tomáš Oberhuber's avatar Tomáš Oberhuber Committed by Tomáš Oberhuber
Browse files

Added FileCloseError exception.

parent 0b81f7e7
No related branches found
No related tags found
1 merge request!29Revision
/***************************************************************************
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
......@@ -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() ) {
file.close();
if( ! file.good() ) {
std::cerr << "I was not able to close the file " << fileName << " properly!" << std::endl;
return false;
if( file.is_open() )
{
try
{
file.close();
}
catch(...)
{
throw Exceptions::FileCloseError( fileName );
}
}
// reset all attributes
// reset file name
fileName = "";
return true;
}
......
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