What's better/faster? Try-catch or avoid exception?

前端 未结 8 909
花落未央
花落未央 2021-01-08 01:32

I would like to know, what\'s better or/and faster in general programming? Avoid the exception or wait for the exception?

Avoid the exception would be:



        
8条回答
  •  梦毁少年i
    2021-01-08 01:49

    Exceptions should only be used for exceptional conditions (unless there's no alternative), so you should only use try/catch when there's something highly unusual going on that you still need to deal with (pop up an error message).

    Having a try/catch also signals to a programmer that some external error might happen and needs to be dealt with. In your example, list being null is simply a normal part of program execution/control flow, and so there's nothing exceptional about it.

    Also, an empty catch-all is generally a bad thing to have anyway (although there are limited situations where it's needed). It should certainly need a comment anyway to explain why you're not doing anything about the exceptions.

    There's a useful blog post about vexing exception you might find useful

提交回复
热议问题