Are exceptions in php really that useful?

后端 未结 9 2315
猫巷女王i
猫巷女王i 2021-02-08 14:48

3 days ago I started rewriting one of my scripts in OOP using classes as a practice after reading a lot about the advantages of using OOP.

Now I\'m confused weather I sh

9条回答
  •  灰色年华
    2021-02-08 15:32

    You shouldn't use exceptions when your code can handle the error gracefully using an if statement (like you did there on your example).

    Exceptions are for situation that are well, exceptional. Unfortunately this is not very straightforward, so it's up to you, the programmer, to decide what is exceptional or not. I think a good rule of thumb is:

    Avoid using exceptions to indicate conditions that can reasonably be expected as part of the typical functioning of the method.

    From: http://www.codeproject.com/KB/dotnet/When_to_use_exceptions.aspx

提交回复
热议问题