Skip to main content
broken link fixed, cf. https://meta.stackoverflow.com/a/406565/4751173
Source Link
Glorfindel
  • 3.2k
  • 6
  • 26
  • 33

To address the part of your question about what exceptions you should catch, have a look at this article:

How to Design Exception HierarchiesHow to Design Exception Hierarchies

Despite the title, it's very useful in understanding the different types of exceptions. There are three types, and they should be handled in three different ways:

  • Usage error. This is due to an error in coding - fix your code.
  • Logical error. An unavoidable condition, such as file not found - handle the exception.
  • System failure. A system error such as out of memory - shut down the program.

(File not found is unavoidable because even if you call File.Exists(), it's possible someone could delete the file just before you access it. So to really be correct, you still need to handle it.)

The only case where you definitely need try/catch blocks is for logical errors. In those cases, you can't guarantee avoiding the exception, so you need to handle it.

To address the part of your question about what exceptions you should catch, have a look at this article:

How to Design Exception Hierarchies

Despite the title, it's very useful in understanding the different types of exceptions. There are three types, and they should be handled in three different ways:

  • Usage error. This is due to an error in coding - fix your code.
  • Logical error. An unavoidable condition, such as file not found - handle the exception.
  • System failure. A system error such as out of memory - shut down the program.

(File not found is unavoidable because even if you call File.Exists(), it's possible someone could delete the file just before you access it. So to really be correct, you still need to handle it.)

The only case where you definitely need try/catch blocks is for logical errors. In those cases, you can't guarantee avoiding the exception, so you need to handle it.

To address the part of your question about what exceptions you should catch, have a look at this article:

How to Design Exception Hierarchies

Despite the title, it's very useful in understanding the different types of exceptions. There are three types, and they should be handled in three different ways:

  • Usage error. This is due to an error in coding - fix your code.
  • Logical error. An unavoidable condition, such as file not found - handle the exception.
  • System failure. A system error such as out of memory - shut down the program.

(File not found is unavoidable because even if you call File.Exists(), it's possible someone could delete the file just before you access it. So to really be correct, you still need to handle it.)

The only case where you definitely need try/catch blocks is for logical errors. In those cases, you can't guarantee avoiding the exception, so you need to handle it.

Source Link
Kyralessa
  • 3.7k
  • 2
  • 21
  • 20

To address the part of your question about what exceptions you should catch, have a look at this article:

How to Design Exception Hierarchies

Despite the title, it's very useful in understanding the different types of exceptions. There are three types, and they should be handled in three different ways:

  • Usage error. This is due to an error in coding - fix your code.
  • Logical error. An unavoidable condition, such as file not found - handle the exception.
  • System failure. A system error such as out of memory - shut down the program.

(File not found is unavoidable because even if you call File.Exists(), it's possible someone could delete the file just before you access it. So to really be correct, you still need to handle it.)

The only case where you definitely need try/catch blocks is for logical errors. In those cases, you can't guarantee avoiding the exception, so you need to handle it.