是否有事实上的标准例外或最佳实践方法来发出异常"invalid input"信号?
Is there a defacto standard exception or best-practice way in signalling "invalid input" by exception?
invalid_argument
is-a - logic_error
。
两个参考点都表示
行上的内容这个类
[i.e. logic_error]
定义了作为异常抛出的对象类型,用于报告程序内部逻辑中的错误,例如违反逻辑前提条件或类不变量。这些错误可以在程序执行之前检测到。
问题:接受invalid_argument
的语义严格约束于"程序员的错误",是否有任何事实上的标准异常程序/库/服务器可以使用++在运行时与外部调用方通信"提供的输入无效"?
您以前的经验是否显示了使用"invalid_input"类型异常的常规模式?
如果是这样,它是标准的还是每个人都只是根据需要派生他们的异常?
指出:
显然,
stdexcept
没有提供runtime语义。boost虽然我知道/遵循尽可能早的验证规则,但我发现经常有输入数据(无论是否来自用户)只能在更深的上下文中进行验证的情况:尽可能早的可能需要与更深的上下文中进行匹配,而"在网关"不可用。
++ "to communication " =在调用链中(或者在堆栈跟踪中,如果你喜欢的话)有一个像
这样的块// fwd declaration
void function_facing_dirty_code(struct user_input& data);
void function_facing_the_user(const char* jsonArgs) {
try {
struct user_input;
parse_user_input(json_args, user_input);
function_facing_dirty_code(user_input);
}
catch(invalid_input& ii) {
// **this** should be a standard error for erroneous input/args/etc
// treat it by telling the out-of-my-control caller to behave
}
catch (std::runtime_exception& e) {
// tell the caller: sorry, you've done nothing wrong,
// but I'm having generic runtime troubles.
}
}
void function_facing_dirty_code(struct user_input& data) {
try {
// ... do some work
// ... do some more work
// Ahhh
throw std::invalid_argument("Requested amount over the daily redraw limit");
// ooops. This will cause a BSoD instead of telling the user.
// Because std::invalid_argument is a logic error
}
catch(std::logic_error& bsod) {
// log an error, blame the author, snitch it to his boss,
// then generate a BSoD for the user's delight,
// because she must NOT see our coding family's dirty laundry
}
}
std::logic_error
和派生类背后的想法有点像一个美化的断言——在标准库中,它们的使用方式有点像(想想vector::at
——你想从无效的向量索引中获得什么样的恢复?这只是意味着逻辑是拙劣的,它只是一个友好的替代UB);invalid_argument
也用于类似的事情,IIRC在std::bitset
。
但是我同意,一般来说,"无效参数"类型的异常可以派生自"逻辑"或"运行时"错误;我在这里的主要观点是:不要在没有逻辑的地方寻求太多逻辑。
整个<stdexcept>
的事情看起来像是在晚餐前草草记下来的,然后完全忘记了。非常广泛的错误类别被直接划分为逻辑/运行时二分法(有争议的价值),本质上是基于它们如何在标准库中被使用;那些没有被使用的(这应该是一个方便的程序员)是随机的选择(overflow
/underflow
/domain_error
-也许他们只是在考虑数学函数?)和表征(out_of_range
是"逻辑",但overflow
是"运行时"?
简而言之:不要过度思考stdexcept
的想法-它们是模糊的,有争议的,然后被锤打以适应标准库的一些需求。从std::exception
或std::runtime_error
派生出对您的应用程序有意义的异常层次结构,并满意地使用它。
- 处理多个异常集合的C++方法
- 我在c++代码中生成了一个运行时#3异常
- 孤立代码块在结构中引发异常
- C++中的赋值发生,尽管右侧出现异常
- 从构造函数抛出异常时如何克服内存泄漏
- 异常属于C++中的线程还是进程
- 当类定义不可见时捕获异常
- 引发异常:读取访问冲突**dynamicArray**为0x1118235.发生
- 如何解决"invalid conversion from 'char' to 'const char*'"
- 为什么异常不退出程序?
- 为什么我应该在异常处理中使用std::cerr而不是std::cout
- 如何修复链表类实现的未处理异常0xDDDDDDDD
- 关于:C++中异常对象的范围:为什么我没有得到副本?
- 如何处理 c++ 中类实现中的"invalid use of non-static data member"?
- 是什么导致了Unity 3D中的"错误线程异常"?
- 如何将strftime中的格式错误作为异常捕获
- 创建具有 new in 函数和"this is nullptr"异常的对象
- 尝试使用智能指针时引发异常
- 防止"Float-point invalid operation"异常?
- 是否有事实上的标准例外或最佳实践方法来发出异常"invalid input"信号?