When should I use a ThrowHelper method instead of throwing directly?

前端 未结 6 1740
傲寒
傲寒 2021-02-05 10:16

When is it appropriate to use a ThrowHelper method instead of throwing directly?

void MyMethod() {
    ...
    //throw new ArgumentNullException(\"param         


        
6条回答
  •  故里飘歌
    2021-02-05 10:36

    This seems like a needless abstraction to me. You have a method that does one thing, named as verbosely as the thing that it does.

    The argument about less bytecode is practically meaningless since the size of your code rarely matters (you wouldn't be saving more than a kilobyte per instance of your program, unless you throw that exception from a ridiculous number of places in your source code). Meanwhile your stated drawbacks are all real concerns, especially where clarity of exception handling is concerned.

    Basically abstracting away minor things like this usually comes back to bite you. (Some entertaining reading on the subject: http://www.joelonsoftware.com/articles/LeakyAbstractions.html)

提交回复
热议问题