C#: Throwing Custom Exception Best Practices

前端 未结 8 915
没有蜡笔的小新
没有蜡笔的小新 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:03

    Note Option 1: your throw new FooException("Reason..."); won't be caught as it's outside try / catch block

    1. You should be only catching exceptions that you want to process.
    2. If you're not adding any additional data to the exception than use throw; as it won't kill your stack. In Option 2 you still might do some processing inside catch and just call throw; to rethrow original exception with original stack.
    0 讨论(0)
  • 2020-11-30 02:10

    if you run the code snippet for 'Exception' in Visual Studio you have a template of a good practice exception writing.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-30 02:21

    Have a look at this MSDN-best-practises.

    Consider to use throw instead of throw ex if you want to re-throw caught exceptions, because on this way the original stacktrace keeps preserved(line numbers etc.).

    0 讨论(0)
  • 2020-11-30 02:23

    I always add a couple of properties when creating a custom exception. One is user name or ID. I add a DisplayMessage property to carry text to be displayed to the user. Then, I use the Message property to convey technical details to be recorded in the log.

    I catch every error in the Data Access Layer at a level where I can still capture the name of the stored procedure and the values of the parameters passed. Or the inline SQL. Maybe the database name or partial connection string (no credentials, please). Those may go in Message or in their own new custom DatabaseInfo property.

    For web pages, I use the same custom exception. I'll put in the Message property the form information -- what the user had entered into every data entry control on the web page, the ID of the item being edited (customer, product, employee, whatever), and the action the user was taking when the exception occurred.

    So, my strategy as per your question is: only catch when I can do something about the exception. And quite often, all I can do is log the details. So, I only catch at the point where those details are available, and then rethrow to let the exception bubble up to the UI. And I retain the original exception in my custom exception.

    0 讨论(0)
  • 2020-11-30 02:23

    The purpose of custom exceptions is to provide detailed, contextual information to the stacktrace to aid in debugging. Option 1 is better because without it, you don't get the "origin" of the exception if it occurred "lower" in the stack.

    0 讨论(0)
提交回复
热议问题