From 0b81f7e7e78aa60c93464b3033291f6fd24f782e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= <oberhuber.tomas@gmail.com>
Date: Tue, 5 Mar 2019 21:33:03 +0100
Subject: [PATCH] Added FileOpenError exception.

---
 src/TNL/Exceptions/FileOpenError.h | 32 ++++++++++++++++++++++++++++++
 src/TNL/Exceptions/NotTNLFile.h    |  2 +-
 src/TNL/File.hpp                   | 19 +++++++++---------
 3 files changed, 42 insertions(+), 11 deletions(-)
 create mode 100644 src/TNL/Exceptions/FileOpenError.h

diff --git a/src/TNL/Exceptions/FileOpenError.h b/src/TNL/Exceptions/FileOpenError.h
new file mode 100644
index 0000000000..3975c267e6
--- /dev/null
+++ b/src/TNL/Exceptions/FileOpenError.h
@@ -0,0 +1,32 @@
+/***************************************************************************
+                          FileOpenError.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 FileOpenError
+   : public std::runtime_error
+{
+public:
+   FileOpenError( const String& fileName )
+   : std::runtime_error( "Unable to open file " + fileName + "." )
+   {}
+};
+
+} // namespace Exceptions
+} // namespace TNL
diff --git a/src/TNL/Exceptions/NotTNLFile.h b/src/TNL/Exceptions/NotTNLFile.h
index e65fd490b2..ce2d756a53 100644
--- a/src/TNL/Exceptions/NotTNLFile.h
+++ b/src/TNL/Exceptions/NotTNLFile.h
@@ -23,7 +23,7 @@ class NotTNLFile
 {
 public:
    NotTNLFile()
-   : std::runtime_error( "Wring magic number found in a binary file. It is not TNL compatible file." )
+   : std::runtime_error( "Wrong magic number found in a binary file. It is not TNL compatible file." )
    {}
 };
 
diff --git a/src/TNL/File.hpp b/src/TNL/File.hpp
index 257dd8da8c..c89afa567d 100644
--- a/src/TNL/File.hpp
+++ b/src/TNL/File.hpp
@@ -19,6 +19,7 @@
 #include <TNL/Exceptions/MICSupportMissing.h>
 #include <TNL/Exceptions/FileSerializationError.h>
 #include <TNL/Exceptions/FileDeserializationError.h>
+#include <TNL/Exceptions/FileOpenError.h>
 
 namespace TNL {
 
@@ -39,18 +40,16 @@ inline bool File::open( const String& fileName, Mode mode )
    if( mode & Mode::Append ) ios_mode |= std::ios::app;
    if( mode & Mode::AtEnd ) ios_mode |= std::ios::ate;
    if( mode & Mode::Truncate ) ios_mode |= std::ios::trunc;
-   file.open( fileName.getString(), ios_mode );
-   
-   /*if( mode == Mode::In )
-      file.open( fileName.getString(), std::ios::binary | std::ios::in );
-   else
-      file.open( fileName.getString(), std::ios::binary | std::ios::out );*/
+   try
+   {
+      file.open( fileName.getString(), ios_mode );
+   }
+   catch(...)
+   {
+      throw Exceptions::FileOpenError( fileName );
+   }
 
    this->fileName = fileName;
-   if( ! file.good() ) {
-      std::cerr << "I am not able to open the file " << fileName << ". ";
-      return false;
-   }
    return true;
 }
 
-- 
GitLab