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

前端 未结 11 1296
抹茶落季
抹茶落季 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条回答
  •  一整个雨季
    2020-12-30 08:54

    As Kyle said, make your methods small in length, put try/catch around small areas only. Hover the mouse over the methods that you call - you should get a list of exceptions then. This will not catch every exception listed, but Exceptions can also be discovered empirically if you print the exception type inside of your catch (Exception e) { ... }. What you are after is e.GetType().FullName and e.StackTrace and e.Message and e.InnerException ... or a subset of what I listed.

提交回复
热议问题