Подтвердить что ты не робот

Какой тип исключения я должен бросить?

Перейдя через некоторые ссылки на обработку исключений (1, 2, и 3), я знаю, что программы на С++ могут вообще что-то делать как исключения (int, char*, string, exception). Я знаю, что std::exception - это базовый класс для стандартных исключений, создаваемых программой. Тем не менее, я пытаюсь создать блок try... catch как таковой:

try
{
    MyFunc();
}
catch (certain exceptions)
{
    // deal with the exception accordingly
}
catch (the rest of the exceptions)
{
    // deal with these accordingly
}

while MyFunc() содержит следующее:

void MyFunc()
{
    ...
    if (certain condition is true) throw exception;
    ...
}

Проблема в том, что в этой части функции MyFunc я не уверен, какой тип исключения я должен бросить. Чтобы сохранить код чистым, реализуя мой собственный класс исключений, я понятия не имею, что было бы хорошим способом реализовать такой класс исключений.

4b9b3361

Ответ 1

Вы получите свой собственный класс из std::exception, так что есть способ равномерного обработки исключений.

Если это кажется излишним, вы можете бросить std::logic_error или один из других стандартных типов исключений, предназначенных для использования приложениями.

Вы также можете использовать их в качестве базовых классов для своих собственных более конкретных исключений: это экономит небольшую работу, потому что они заботятся о реализации метода what для вас.

Обратите внимание, что глубокие иерархии исключений могут быть непригодными для использования, поскольку вы в основном заранее задумываетесь о том, как классифицировать ошибки, и ваши клиенты могут не согласиться.

Ответ 2

Вот фрагмент кода, который показывает, как расширить и использовать класс std:: exception: (Кстати, у этого кода есть ошибка, о чем я расскажу позже).

#include <iostream>
#include <string>
#include <exception>

class my_exception : public std::exception
{
public:
   explicit my_exception(const std::string& msg)
      : msg_(msg)
   {}

   virtual ~my_exception() throw() {}

   virtual const char* what() const throw()
   {
      return msg_.c_str();
   }

private:
   std::string msg_;
};

void my_func() throw (my_exception&)
{
  throw my_exception("aaarrrgggg...");
}

int
main()
{
  try
    {
      my_func();
    }
  catch (my_exception& ex)
    {
      std::cout << ex.what() << '\n';
    }
  return 0;
}

Обратите внимание, что конструктор является явным, а деструктор и what() объявлены (используя throw()), чтобы указать, что они сами не будут генерировать исключения. Вот где ошибка. Гарантировано ли, что вызов msg_.c_str() не будет генерировать собственные исключения? Как насчет конструктора строк, который мы используем для инициализации msg_? Это может также вызвать исключения. Как мы можем создать класс исключений, который безопасен для исключений, вызванных объектами-членами? Ответ - наследовать от std:: runtime_error или подобный подкласс std:: exception. Таким образом, правильный способ реализовать my_exception будет следующим:

class my_exception : public std::runtime_error
{
public:
    my_exception(const std::string& msg) 
        : std::runtime_error(msg)
    { }
};

Нам не нужно переопределять what(), поскольку он уже реализован в std:: runtime_error. Правильная обработка буфера сообщений выполняется с помощью std:: runtime_error, поэтому мы можем быть уверены, что сама my_exception не будет вызывать неизвестную ошибку во время выполнения.

Ответ 3

Я подумал, что было бы интересно опубликовать какой-нибудь реальный код для изменения. Это класс исключений, который использует моя собственная библиотека:

//---------------------------------------------------------------------------
// a_except.h
//
// alib exception handling stuff
//
// Copyright (C) 2008 Neil Butterworth
//---------------------------------------------------------------------------

#ifndef INC_A_EXCEPT_H
#define INC_A_EXCEPT_H

#include "a_base.h"
#include <exception>
#include <sstream>

namespace ALib {

//------------------------------------------------------------------------
// The only exception thrown directly by alib
//------------------------------------------------------------------------

class Exception : public std::exception {

    public:

        Exception( const std::string & msg = "" );
        Exception( const std::string & msg, int line,
                        const std::string & file );

        ~Exception() throw();

        const char *what() const throw();
        const std::string & Msg() const;

        int Line() const;
        const std::string & File() const;

    private:

        std::string mMsg, mFile;
        int mLine;
};

//------------------------------------------------------------------------
// Macro to throw an alib exception with message formatting.
// Remember macro is not in ALib namespace!
//------------------------------------------------------------------------

#define ATHROW( msg )                                               \
{                                                                   \
    std::ostringstream os;                                          \
    os << msg;                                                      \
    throw ALib::Exception( os.str(), __LINE__, __FILE__ );          \
}                                                                   \


}  // namespace

#endif

И это файл .cpp:

//---------------------------------------------------------------------------
// a_except.h
//
// alib exception handling stuff
//
// Copyright (C) 2008 Neil Butterworth
//---------------------------------------------------------------------------

#include "a_except.h"
using std::string;

namespace ALib {

//---------------------------------------------------------------------------
// exception with optional message, filename & line number
//------------------------------------------------------------------------

Exception :: Exception( const string & msg ) 
    : mMsg( msg ),  mFile( "" ), mLine(0) {
}

Exception :: Exception( const string & msg, int line, const string & file ) 
    : mMsg( msg ), mFile( file ), mLine( line ) {
}

//---------------------------------------------------------------------------
// Do nothing
//---------------------------------------------------------------------------

Exception :: ~Exception() throw() {
}

//------------------------------------------------------------------------
// message as C string via standard what() function
//------------------------------------------------------------------------

const char * Exception :: what() const throw() {
    return mMsg.c_str();
}

//------------------------------------------------------------------------
// as above, but as C++ string
//------------------------------------------------------------------------

const string & Exception :: Msg() const {
    return mMsg;
}

//---------------------------------------------------------------------------
// File name & line number
//---------------------------------------------------------------------------

int Exception :: Line() const {
    return mLine;
}

const string & Exception :: File() const {
    return mFile;
}

}  // namespace

// end

Ответ 4

Если вы можете использовать boost, тогда вы должны это сделать. Обратитесь к этой ссылке о том, как использовать исключения форсирования. Вы также можете создать свою собственную иерархию классов исключений, как указано в других ответах, но вам нужно позаботиться о тонких аспектах, таких как требования "nothrow" от метода "что". Ниже описывается базовый проект о том, как это можно сделать в строках boost:: exception: -

#include <string>
#include <memory>
#include <stdexcept>

/************************************************************************/
/* The exception hierarchy is devised into 2 basic layers. 
   System exceptions and Logic exceptions. But Logic exceptions are 
   convertible to the ultimate base 'System' in the system layer.
*************************************************************************/

// the system exception layer
  namespace ExH
  {
    namespace System {
      // This is the only way to make predefined exceptions like
      // std::bad_alloc, etc to appear in the right place of the hierarchy.
      typedef std::exception Exception;
      // we extend the base exception class for polymorphic throw
      class BaseException : public Exception {
      public:
        BaseException() throw() {}
        explicit BaseException(char const* /*desc*/) throw() 
          : Exception()
        {}
        BaseException(BaseException const& that)
          : Exception(that)
        {}
        virtual void raise() const { throw *this; } // used to throw polymorphically
        virtual ~BaseException() throw () {}
      };
      // module level classes compose and catch the descriptive
      // versions of layer-exceptions
      class DescriptiveException : public BaseException  {
      public:
        explicit DescriptiveException (char const* description) throw()
          : description_(description)
        { }
        explicit DescriptiveException (std::string const& description) throw()
          : description_(description.c_str())
        { }

        virtual ~DescriptiveException () throw () {} 

        DescriptiveException (DescriptiveException const& src) throw()
          : BaseException(src)
        {
          this->description_ = src.description_;
        }
        DescriptiveException& operator= (DescriptiveException const& src) throw()
        {
            if (this != &src)
            {
              this->description_ = src.description_;
            }
            return *this;
        }

        /*virtual*/ char const* what () const throw() { return description_; }
        /*virtual*/ void raise() const // used to throw polymorphically
        { throw *this; }
      protected:
        DescriptiveException () throw ();
      private:
        char const* description_; 
      };

    }
  }

// the logic exception layer
  namespace ExH
  {
    namespace Logic
    {

      // Logic::Exception inherits from System::Exception for the
      // following reason. Semantically for some part of the
      // system particular instance of Logic::Exception may seem as
      // opaque System::Exception and the only way to handle it would
      // be to propagate it further. In other words Logic::Exception
      // can be seamlessly "converted" to System::Exception if there is
      // no part of the system interested in handling it.
      //
      class BaseException : public System::BaseException
      {
      public:
        BaseException() throw() {}
        explicit BaseException(char const* desc) throw() 
          : System::BaseException(desc)
        {}
        BaseException(BaseException const& that)
          : System::BaseException(that)
        {}
        virtual void raise() const { throw *this; } // used to throw polymorphically
        virtual ~BaseException() throw () {}
      };
      // module level classes compose and catch the descriptive
      // versions of layer-exceptions
      class DescriptiveException : public BaseException {
      public:
        explicit
        DescriptiveException (char const* description) throw()
          : description_(new std::string(description))
        { }
        explicit
        DescriptiveException (std::string const& description) throw()
          : description_(new std::string(description))
        { }
        DescriptiveException(DescriptiveException const& src) throw()
          : BaseException(src)
        {
            // copy the string
            std::string* str = new std::string(src.description_.get()->c_str());
            description_.reset(str);
        }

        virtual ~DescriptiveException () throw () {}
        /*virtual*/ char const* what () const throw() { return description_->c_str(); }
        /*virtual*/ void raise() const { throw *this; }
      private:
        DescriptiveException& operator= (DescriptiveException const& src) throw(); // copy disabled
        std::auto_ptr<std::string> description_; // do not use std::string, as it can throw
      };
    }
  }


/************************************************************************/
/* Users of the exception hierarchy compose specific exceptions as and
when needed. But they can always be caught at the System::Exception base
class level. Some of the standard conversion examples are demonstrated :-

class MyClass {
public:
  class Exception_ {};
  typedef
  Compound <Exception_, Logic::DescriptiveException>
  Exception;

  class InvalidArgument_ {};
  typedef
  Compound <InvalidArgument_, Exception>
  InvalidArgument;

  class NotInitialized_ {};
  typedef
  Compound <NotInitialized_, Exception>
  NotInitialized;
public:
  void myFunction1() const throw(NotInitialized);
  void myFunctionN() const throw(NotInitialized);
};

void MyClass::myFunction1() const {
  throw NotInitialized("Not Inited!");
}

void MyClass::myFunctionN() const {
  try {
    // call myFunction1()
  }
  catch(NotInitialized const& e){
  // use e
  }
}

This has to be per-class basis. The exposed module will have an exception
specification which will catch all the sub-class exceptions. The calling
module will in turn rely on this exception-specification. This will allow
us to have generalized exception-catching at the application-level and
more specialized exception-catching at the specific module level.       */
/************************************************************************/

// a simple template to compose the exceptions as per conversion requirements
  namespace ExH
  {
    template <typename Type, typename Base>
    class Compound : public Base
    {
    public:
      explicit Compound (char const* description) throw ()
        : Base(description)
      {}
      explicit Compound (std::string const& description) throw ()
        : Base(description)
      {}

      Compound (Compound const& src) throw ()
        : Base(src)
      {}

      virtual ~Compound () throw () {}
    protected:
      Compound () throw () {}
    private:
      Compound& operator= (Compound const& src) throw (); // disable copy
    };

  }

Ответ 5

Обычно вы должны получать свои собственные классы исключений из std:: exception и его производных для представления ошибок, относящихся к вашему домену приложения, например, если вы имеете дело с файлами, у вас должно быть FileNotFoundException, которое включает путь к файлу и другую соответствующую информацию, таким образом вы можете создавать блоки catch для обработки определенных типов ошибок и опускать других, вы также должны воздерживаться от бросания или ловушки исключений, отличных от класса.

Посмотрите на аналогичные иерархии исключений в . NET и Java, чтобы посмотреть, как моделировать общие ошибки (ошибки файлов, ошибки ввода-вывода, сеть ошибки и т.д.)