什么时候和如何使用异常处理? [英] When and how should I use exception handling?

查看:176
本文介绍了什么时候和如何使用异常处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读关于异常处理。我得到一些有关异常处理的信息,但我有几个问题:

I am reading about exception handling. I got some information about what exception handling is, but I have a few questions:


  1. 何时抛出异常?

  2. 我们可以使用返回值来表示错误吗?

  3. 如果我通过try-catch块保护所有的函数,

  4. 我看到一个项目,其中该项目中的每个函数都包含一个try-catch块(即整个函数内部的代码被try-catch块包围)。这是一个好的做法吗?

  5. try-catch和__try __except有什么区别?

  1. When to throw an exception?
  2. Instead of throwing an exception, can we use a return value to indicate the error?
  3. If I protect all my functions by try-catch blocks, won't it reduce the performance?
  4. When to use exception handling?
  5. I saw a project where each and every function in that project contained a try-catch block (i.e. code inside the entire function is surrounded by try-catch block). Is this a good practice?
  6. What is the difference between try-catch and __try __except?


推荐答案

这里是相当全面的指南,我认为是必须阅读:

Here's quite comprehensive guide on exceptions that I think is a Must Read:

异常和错误处理 - C++常见问题 C ++ FAQ lite

Exceptions and error handling - C++ FAQ or C++ FAQ lite

作为一般的经验法则,当你的程序可以识别一个外部阻止执行的问题。如果从服务器接收数据并且该数据无效,则抛出异常。磁盘空间不足?抛出异常。宇宙射线阻止你查询数据库?抛出异常。但是如果你从你自己的程序里面得到一些无效的数据 - 不要抛出异常。如果你的问题来自你自己的坏代码,最好使用ASSERTs来防范它。需要异常处理来识别程序不能处理的问题,并告诉他们关于用户,因为用户可以处理它们。但是你的程序中的错误不是用户可以处理的,所以程序崩溃将告诉没有什么比answer_to_life_and_universe_and_everything的值不是42!这应该永远不会发生!!!! 11异常。

As a general rule of thumb, throw an exception when your program can identify an external problem that prevents execution. If you receive data from the server and that data is invalid, throw an exception. Out of disk space? Throw an exception. Cosmic rays prevent you from querying the database? Throw an exception. But if you get some invalid data from inside your very own program - don't throw an exception. If your problem comes from your own bad code, it's better to use ASSERTs to guard against it. Exception handling is needed to identify problems that program cannot handle and tell them about the user, because user can handle them. But bugs in your program are not something the user can handle, so program crashing will tell not much less than "Value of answer_to_life_and_universe_and_everything is not 42! This should never happen!!!!11" exception.

捕获异常,您可以使用它做一些有用的事情,例如,显示一个消息框。我喜欢在一个函数内部捕获异常,以某种方式处理用户输入。例如,用户按下按钮Annihilate all hunams,在里面的annihilateAllHunamsClicked()函数有一个try ... catch块说我不能。即使hunamkind的湮灭是一个复杂的操作,需要调用几十和几十个函数,只有一个try ... catch,因为对于一个用户,它是一个原子操作 - 一个按钮单击。

Catch an exception where you can do something useful with it, like, display a message box. I prefer to catch an exception once inside a function that somehow handles user input. For example, user presses button "Annihilate all hunams", and inside annihilateAllHunamsClicked() function there's a try...catch block to say "I can't". Even though annihilation of hunamkind is a complex operation that requires calling dozens and dozens of functions, there is only one try...catch, because for a user it's an atomic operation - one button click. Exception checks in every function are redundant and ugly.

此外,我不能推荐足够熟悉RAII - 也就是说,确保所有被初始化的数据自动销毁。这可以通过在堆栈上尽可能多地初始化来实现,当你需要在堆上初始化一些东西时,使用某种智能指针。当引发异常时,在堆栈上初始化的所有内容都将被自动销毁。如果你使用C风格的哑指针,当抛出异常时会冒内存泄漏的风险,因为没有人在异常时清理它们(确实,你可以使用C风格的指针作为类的成员,但要确保它们是在破坏者中照顾)。

Also, I can't recommend enough getting familiar with RAII - that is, to make sure that all data that is initialized is destroyed automatically. And that can be achieved by initializing as much as possible on stack, and when you need to initialize something on heap, use some kind of smart pointer. Everything initialized on the stack will be destroyed automatically when an exception is thrown. If you use C-style dumb pointers, you risk memory leak when an exception is thrown, because there is noone to clean them up upon exception (sure, you can use C-style pointers as members of your class, but make sure they are taken care of in destructor).

这篇关于什么时候和如何使用异常处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆