그런 일은 결코 시도하지 않았지만 여기에 내가 할 일이 있습니다.
오류 유형 :
extern const bool exceptionmode;
enum error_codes {no_error, whatever};
struct error_type {
error_codes id;
//maybe also a non-owning const char* message?
error_type() :id(no_error) {}
error_type(error_codes code) :id(code)
{
if (exceptionmode) throw_as_exception();
}
const char* get_as_string() {
switch(id) {
case whatever: return "out of bounds exception";
case no_error: return "no error";
default: return "unknown exception";
}
}
void throw_as_exception() {
switch(id) {
case whatever: throw std::out_of_bounds("out of bounds exception");
case no_error: break; //no error
default: throw std::exception("unknown exception");
}
}
};
클래스와 함수가 : 당신이 정말 용감한있어 경우 ERROR_TYPE 클래스는 주장 있도록 비교하거나 변환됩니다 않을 경우
class myclass {
public:
error_type my_member_function() {
//stuff
if (condition)
return whatever;
//more stuff
return no_error;
}
};
, 당신이 그것을 만들 수 있습니다 또는 아무도 반환 값을 확인하는 것을 잊어 버리지 않았는지 확인하기 위해 검사했습니다.
'예외를 사용하지 않습니다.'라는 말은 (1) 언어 구성으로 허용되지 않습니다. RTTI가 비활성화되어 있거나 (2) 사용자가 코드에서 RTTI를보고 싶지 않은 경우? (2) 또한 "내부"코드에 영향을 미칩니다. – pmr
(2). 맞습니다. public 멤버 함수 내에서 사용할 때 내부 코드는 항상 try-catch 블록 안에 있습니다. – Xyand
글쎄, 만약 당신이 2 클래스와 외관과 함께 당신은 거기에 기본 유형과 파생 된 유형 사이에 절대 변환이 있는지 확인해야합니다. 반환 유형이 함께 사용할 수 없으므로 이러한 함수를 가상으로 만들 수는 없습니다. 나는 3 개의 클래스로 갈 것이다. 'throw '포인트가 함수의 중간에있을 수 있고 코드를 효율적으로 재사용 할 수 없기 때문에 이것은 여전히 성가신 일이지만, 그것이 당신이 얻는 것입니다. – pmr