Is python exception handling more efficient than PHP and/or other languages?

前端 未结 6 993
遥遥无期
遥遥无期 2021-01-03 21:32

I have it drilled into my head that (at least in PHP) it is badbadmojo to use try... catch blocks for flow control. What I\'ve learned is to use them only to h

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-03 21:49

    I've done a bit more reading, and have realized one other important thing no one has mentioned yet: in Python, there is no distinction between Errors and Exceptions. I knew this, but I don't think I had grasped the significance until now.

    In PHP, you can wrap $page->GetContent(); in try...catch blocks all you want, but if $page is not an object, your application will still come to a screeching halt with the resulting fatal error. So in PHP (and any other language where errors cannot be caught), you need a certain level of LBYL.

    Python basically says, "that's stupid, I should be able to catch those." Just try to do what you need to do, and deal with any fallout in the except block.

    Here is a great article on exception handling I came across.

提交回复
热议问题