Python try except

后端 未结 5 1575
[愿得一人]
[愿得一人] 2021-01-19 00:12
try:
    #statement 1
    #statement 2
except Exception, err:
    print err
    pass

This may be very trivial but I never actually thought about it

5条回答
  •  情深已故
    2021-01-19 01:03

    1) Does statement 2 gets executed if an error is raised in statement 1?

    No. Exception will be raised and catched. As I understand python will move up the stack and looks for an exception handler in the caller

    2) How does Exception deal with in a case where an error is raised for both statement 1 and statement 2? Which error does it print out in the above code? both?

    statement 2 will not be run so no exceptions will be raised for it

    any exception from the try block will be caught. That is why for all try/except clauses, limit the try clause to the absolute minimum amount of code necessary. Again, this avoids masking bugs.

提交回复
热议问题