Boost logo

Boost Users :

From: Stephen torri (storri_at_[hidden])
Date: 2004-08-24 20:40:45


I thought I would share what I am using for exceptions given the
excellent advice from everyone responding to my initial email. There is
no usages of any memory allocation or std::string like variables.
If there are any 'oops' or 'gotchas' please let me know.

My plan is to use a command flag for taking into a value. The value will
determine what debug messages are reported at the cause of the problem.
The message will be displayed via std::cerr.

Stephen

----------------
Header
----------------
#ifndef IO_EXCEPTION_H_
#define IO_EXCEPTION_H_

#include <exception>
#include <inttypes.h>

namespace kurt {

  /*!
   * \class IOException
   * \brief Rather than inheriting from ifstream this class delegates
   * any required functionality to it.
   * \date 2003
   * \author Stephen Torri
   */
  class IO_Exception : public virtual std::exception {

  public:

    IO_Exception (uint32_t message_id);
    
    /*!
     * \brief Destructor
     */
    virtual ~IO_Exception (void) throw(){}

    /*!
     * \brief Return the extra message accompanying the exception.
     */
    const char* what (void) const throw();

    /*!
     * \brief Return tne name of the exception
     */
    const char* name (void) const throw();

    enum {
      CANNOT_OPEN,
      INPUT_CORRUPTED,
      INVALID_IO_OP,
      READ_ERROR,
      UNSUPPORTED_OP,
      WRITE_ERROR,
    };

  private:

    static const char* m_exception_name;

    static const char* m_messages[];

    uint32_t m_id;

  };

} /* namespace kurt */

-------------
Source
-------------
#include "IOException.hpp"

namespace kurt {

  const char* IO_Exception::m_exception_name = "IO_Exception";

  const char* IO_Exception::m_messages [] = {
    "I/O Error: cannot open", // CANNOT_OPEN
    "Input data is corrupted", // INPUT_CORRUPTED
    "Invalid i/o operation attempted", // INVALID_IO_OP
    "Error reading input", // READ_ERROR
    "Unsupported I/O operation", // UNSUPPORTED_OP
    "Error writing output", // WRITE_ERROR
  };

  IO_Exception::IO_Exception (uint32_t message_id)
    : std::exception(),
      m_id(message_id)
  {}

  const char*
  IO_Exception::what (void) const throw()
  {
    return m_messages[m_id];
  }

  const char*
  IO_Exception::name (void) const throw()
  {
    return m_exception_name;
  }

} /* namespace kurt */

#endif /* IO_EXCEPTION_H_ */

-- 
Email: storri_at_[hidden]

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net