Python using exceptions for control flow considered bad?

前端 未结 3 952
日久生厌
日久生厌 2020-12-02 20:46

All right,

I\'ve seen this multiple times in the past, but most recently with my question here. So, I\'m curious why this is the case, in python because gen

3条回答
  •  悲&欢浪女
    2020-12-02 21:44

    Using try blocks for flow control all the time could produce code like this:

    try:
      # stuff
      try:
        if userCondition:
          throw NeedToDoSomethingElseException
        try:
          # stuff
        except NeedToDoSomethingElseException:
          # other stuff
      except NeedToDoSomethingElseException:
        # other stuff
    except NeedToDoSomethingElseException:
      # other stuff
    

    Performance concerns aside, this is not very elegant. So, sometimes it's perfectly appropriate to use try, but not for everything.

提交回复
热议问题