【发布时间】:2021-06-06 09:33:59
【问题描述】:
我正在使用gcc10.2、c++20。
我在学了 2 年的 python 后正在学习 c++。
在 python 中,我们总是对输入的有效性进行运行时检查
def createRectangle(x, y, width, height): # just for example
for v in [x, y, width, height]:
if v < 0:
raise ValueError("Cant be negative")
# blahblahblah
我将如何在 c++ 中进行这样的处理?
【问题讨论】:
-
raise等效于throw。 -
if(v < 0) throw std::out_of_range("Cant be negative"); -
C++ 是一种错误类型的语言,使用一些
unsigned类型作为参数,所以它们不能是负数:) -
@Jarod42 - 这是我最近看到的最好的错字。
-
@StoryTeller-UnslanderMonica:我强壮吗? :)