C#: Throwing Custom Exception Best Practices

前端 未结 8 949
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 01:25

I have read a few of the other questions regarding C# Exception Handling Practices but none seem to ask what I am looking for.

If I implement my own custom Exception

8条回答
  •  南方客
    南方客 (楼主)
    2020-11-30 02:10

    Option 2 is best. I believe best practice is to only catch exceptions when you plan to do something with the exception.

    In this case, Option 1 just is wrapping an exception with your own exception. It adds no value and users of your class can no longer just catch ArgumentException, for example, they also need to catch your FooException then do parsing on the inner exception. If the inner exception is not an exception they are able to do something useful with they will need to rethrow.

提交回复
热议问题