How to use try, except, else correctly in Python

前端 未结 3 2053
囚心锁ツ
囚心锁ツ 2021-01-04 04:10

So I want to know which is the right way to write try except statements. I\'m new to error handling in Python.

Option 1

try:
    itemCode = items[\"i         


        
3条回答
  •  别那么骄傲
    2021-01-04 04:34

    Simple - it depends. If you are certain about what exceptions you're going to get, you can stick with 1) and frankly, it is the case 90% of the time. The second way is useful if you know that many code routines can raise the same exception type.

提交回复
热议问题