Commit 9aa442c4 authored by Jakub Klinkovský's avatar Jakub Klinkovský
Browse files

Removed superfluous exceptions for file loading/saving

Exceptions are meant to categorize errors, so there should not be many
different exceptions and they should not be specific for a small number
of objects/functions. Let's make all save/load methods throw only 2
exceptions: FileSerializationError and FileDeserializationError.
parent 7f445c6a
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@
#pragma once

#include <iostream>
#include <stdexcept>

#include <TNL/Assert.h>
#include <TNL/Math.h>
@@ -18,7 +19,6 @@
#include <TNL/Containers/Algorithms/ArrayOperations.h>
#include <TNL/Containers/Algorithms/ArrayIO.h>
#include <TNL/Containers/Algorithms/ArrayAssignment.h>
#include <TNL/Exceptions/ArrayWrongSize.h>

#include "Array.h"

@@ -673,7 +673,7 @@ load( File& file )
   Index _size;
   file.load( &_size );
   if( _size < 0 )
      throw Exceptions::ArrayWrongSize( _size, "positive" );
      throw Exceptions::FileDeserializationError( file.getFileName(), "invalid array size: " + std::to_string(_size) );
   setSize( _size );
   if( _size )
      Algorithms::ArrayIO< Value, Device, Index >::load( file, this->data, this->size );
@@ -690,11 +690,11 @@ boundLoad( File& file )
   Index _size;
   file.load( &_size );
   if( _size < 0 )
      throw Exceptions::ArrayWrongSize( _size, "Positive is expected," );
      throw Exceptions::FileDeserializationError( file.getFileName(), "invalid array size: " + std::to_string(_size) );
   if( this->getSize() != 0 )
   {
      if( this->getSize() != _size )
         throw Exceptions::ArrayWrongSize( _size, convertToString( this->getSize() ) + " is expected." );
         throw Exceptions::FileDeserializationError( file.getFileName(), "invalid array size: " + std::to_string(_size) + " (expected " + std::to_string( this->getSize() ) + ")." );
   }
   else setSize( _size );
   if( _size )
+3 −3
Original line number Diff line number Diff line
@@ -11,13 +11,13 @@
#pragma once

#include <iostream>
#include <stdexcept>

#include <TNL/param-types.h>
#include <TNL/ParallelFor.h>
#include <TNL/Containers/Algorithms/ArrayOperations.h>
#include <TNL/Containers/Algorithms/ArrayIO.h>
#include <TNL/Containers/Algorithms/ArrayAssignment.h>
#include <TNL/Exceptions/ArrayWrongSize.h>

#include "ArrayView.h"

@@ -363,11 +363,11 @@ load( File& file )
   String type;
   loadHeader( file, type );
   if( type != SerializationType::getType() )
      throw Exceptions::ObjectTypeMismatch( SerializationType::getType(), type );
      throw Exceptions::FileDeserializationError( file.getFileName(), "invalid object type: " + type + " (expected " + SerializationType::getType() + ")." );
   Index _size;
   file.load( &_size );
   if( _size != this->getSize() )
      throw Exceptions::ArrayWrongSize( _size, convertToString( this->getSize() ) );
      throw Exceptions::FileDeserializationError( file.getFileName(), "invalid array size: " + std::to_string(_size) + " (expected " + std::to_string( this->getSize() ) + ")." );
   Algorithms::ArrayIO< Value, Device, Index >::load( file, this->data, this->size );
}

+0 −32
Original line number Diff line number Diff line
/***************************************************************************
                          ArrayWrongSize.h  -  description
                             -------------------
    begin                : Mar 8, 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 ArrayWrongSize
   : public std::runtime_error
{
public:
   ArrayWrongSize( std::size_t size, const String& mesg = "" )
   : std::runtime_error( "Wrong array size " + convertToString( size ) + ". " + mesg )
   {}
};

} // namespace Exceptions
} // namespace TNL
+2 −2
Original line number Diff line number Diff line
@@ -22,8 +22,8 @@ class FileDeserializationError
   : public std::runtime_error
{
public:
   FileDeserializationError( const std::string& objectType, const std::string& fileName )
   : std::runtime_error( "Failed to deserialize object of type '" + objectType + "' from file '" + fileName + "'." )
   FileDeserializationError( const std::string& fileName, const std::string& details )
   : std::runtime_error( "Failed to deserialize an object from the file '" + fileName + "': " + details )
   {}
};

+2 −2
Original line number Diff line number Diff line
@@ -22,8 +22,8 @@ class FileSerializationError
   : public std::runtime_error
{
public:
   FileSerializationError( const std::string& objectType, const std::string& fileName )
   : std::runtime_error( "Failed to serialize object of type '" + objectType + "' into file '" + fileName + "'." )
   FileSerializationError( const std::string& fileName, const std::string& details )
   : std::runtime_error( "Failed to serialize an object into the file '" + fileName + "': " + details )
   {}
};

Loading