In C#, how do I know which exceptions to catch?

前端 未结 11 1341
抹茶落季
抹茶落季 2020-12-30 08:33

I\'ve gotten in the habit of using a general catch statement and I handle those exceptions in a general manner. Is this bad practice? If so, how do I know which specific exc

11条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-30 09:11

    Except in rare situations, I generally think of catch blocks as a code smell.

    Prevent exceptions being thrown at all by checking for conditions beforehand. For example, if reading from a file, use classes in System.IO to check if the file exists, rather than using a catch block to handle the situation where the file doesn't exist.

    Catch blocks should not form part of your application logic.

提交回复
热议问题