我有一个Exception类,如下所示:
class ExtensionExceptionType;
class Object;
class Exception
{
public:
explicit Exception () { }
Exception( const std::string &reason ) { PyErr_SetString( _Exc_RuntimeError(), reason.c_str() ); }
Exception( PyObject* exception, const std::string &reason ) { PyErr_SetString( exception, reason.c_str() ); }
Exception( PyObject* exception, Object& reason );
Exception( ExtensionExceptionType& exception, const std::string& reason );
Exception( ExtensionExceptionType& exception, Object& reason );
void clear() { PyErr_Clear(); } // clear the error -- technically but not philosophically const
static Object err_type();
static Object err_value();
static Object err_trace();
static Object err_stats( uint32_t i ); // 0 1 2 for {type, value, trace}
static void wrap( int condition ) {
if( condition == -1 )
throw Exception{};
}
};
// Abstract
class StandardError : public Exception { protected: explicit StandardError() {} };
class LookupError : public StandardError { protected: explicit LookupError() {} };
class ArithmeticError : public StandardError { protected: explicit ArithmeticError() {} };
class EnvironmentError : public StandardError { protected: explicit EnvironmentError() {} };
// Concrete (π)
// e.g.
// class TypeError: public StandardError
// {
// public:
// TypeError (const std::string& reason)
// : StandardError()
// {
// PyErr_SetString( Py::_Exc_TypeError(),reason.c_str() );
// }
// };
#define CONCRETE( CLASS, BASE ) \
class CLASS: public BASE \
{ \
public: \
CLASS (const std::string& reason) \
{ \
std::cout << "(Exception.hxx) " #CLASS " from PyCXX (" << reason.c_str() << ") \n"; \
PyErr_SetString( _Exc_##CLASS(), reason.c_str() ); \
} \
};
// it appears that these classes are only for manually RAISING Python errors
// i.e. Raising an exception in the Python runtime
// because if I type something in to the Python console, I can make (e.g.) a KeyError occur, but these classes don't get hit.
CONCRETE( TypeError, StandardError )
CONCRETE( IndexError, LookupError )
CONCRETE( AttributeError, StandardError )
CONCRETE( NameError, StandardError )
CONCRETE( RuntimeError, StandardError )
CONCRETE( NotImplementedError, StandardError )
CONCRETE( SystemError, StandardError )
CONCRETE( KeyError, LookupError )
CONCRETE( ValueError, StandardError )
CONCRETE( OverflowError, ArithmeticError )
CONCRETE( ZeroDivisionError, ArithmeticError )
CONCRETE( FloatingPointError, ArithmeticError )
CONCRETE( MemoryError, StandardError )
CONCRETE( SystemExit, StandardError )
我刚刚补充道:
static void wrap( int condition ) {
if( condition == -1 )
throw Exception{};
}
...因为其他地方有很多次......
if( SomePythonFunc(...) == -1 ) throw Exception{};
......已整理成:
Exception.wrap( SomePythonFunc(...) ); // much nicer, I think
但是,还有以下情况:
if( SomePythonFunc(...) == -1 ) throw TypeError{ "foo" };
......我无法看到如何进行等效包装。
即。写:
TypeError.wrap( SomePythonFunc(...), "foo" );
作为TypeError:Exception,而Exception :: wrap是public,我可以为wrap创建一个可选的第二个参数:
static void wrap( int condition, string err="default-err" ) {
if( condition == -1 )
throw FinalClassConstructor{ err }; // <-- how to do this?
}
...但是我如何调用最终类的构造函数,其中:: wrap刚被命中?
答案 0 :(得分:2)
您要做的是违反多个SOLID原则。你也使你的基类与它的后代紧密耦合(!)。你绝对不希望如此。它应该不知道谁继承了。
要做到这一点,请覆盖子类中的Wrap函数,它们基本上会抛出它们。在您的代码中,您将使用StandardError.Wrap(...
但老实说,我只是在代码中留下例外。
(WHATEVER CONDITION - &gt;抛出异常)在代码中非常精细,并且在使用静态方法的另一个异常中比异常更具可读性。
当你不再重构它时就是这种情况。