Abduaziz Py 🔥 dan repost
💣 No More Bare Excepts
https://peps.python.org/pep-0760
try:
risky_operation()
except:
handle_any_error()
Maqsad yuqoridagidek uslubni yo'qotish. Quyidagini "majburiy" qilish:
try:
risky_operation()
except Exception as e:
handle_expected_error(e)
Dasturchi aynan qaysi exceptionni handle qilayotganini bilishi va kodda ko'rsatishi shart. Lekin hamma holatni qamrab oladigan universal handler ham yozib bo'lmaydi, kutilmagan holatlar juda ham ko'p (MemoryError exception'i uchun handler yozganmisiz hech ?)
Bularni hisobga olgan holda, avvaliga bizga ma'lum bo'lgan barcha exceptionlarni handle qilib, keyin qolgan holatlar uchun kod yozib qo'ysak bo'ladi menimcha:
try:
do_stuff()
except SomeExc:
...
except SomeSomeExc:
...
except Exception as exc:
...
finally:
...
else:
...
try/except:pass yozadiganlarga yuboramiz.
* edit: The syntax will be fully disallowed in Python 3.17.
@AbduazizPy
https://peps.python.org/pep-0760
try:
risky_operation()
except:
handle_any_error()
Maqsad yuqoridagidek uslubni yo'qotish. Quyidagini "majburiy" qilish:
try:
risky_operation()
except Exception as e:
handle_expected_error(e)
Dasturchi aynan qaysi exceptionni handle qilayotganini bilishi va kodda ko'rsatishi shart. Lekin hamma holatni qamrab oladigan universal handler ham yozib bo'lmaydi, kutilmagan holatlar juda ham ko'p (MemoryError exception'i uchun handler yozganmisiz hech ?)
Bularni hisobga olgan holda, avvaliga bizga ma'lum bo'lgan barcha exceptionlarni handle qilib, keyin qolgan holatlar uchun kod yozib qo'ysak bo'ladi menimcha:
try:
do_stuff()
except SomeExc:
...
except SomeSomeExc:
...
except Exception as exc:
...
finally:
...
else:
...
try/except:pass yozadiganlarga yuboramiz.
* edit: The syntax will be fully disallowed in Python 3.17.
@AbduazizPy