Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想知道异常允许我们分开什么?如果您可以使用 c++ 用适当的示例进行解释,那就太好了
异常使你的代码更干净你不需要用if语句来混乱你的代码,只需用try..catch. 因此,他们将您的代码与错误检查分开。
if
try..catch
意图是好的,但异常也带来一些问题,比如忘记用 try..catch 包装代码。Java 使用编译器错误来防止这种情况。
我主要在需要执行许多操作的功能中使用异常,每个操作都取决于前面操作的成功。所以我让所有的动作都抛出异常。我还将它们命名为它们反映了它们抛出异常的事实。