try . . except blokiga else blokini ham qo'shish mumkin. Bu blok hech qanday istisno yuz bermaganda ishga tushadi.
Exception – istino chaqirish
Istisnolarni raise operatori yordamida tegishli xatolik/istisno nomini bergan holda chaqirish mumkin.
Misol: (raising.py nomi bilan saqlang)
class ShortInputException(Exception):
'''Foydalanuvchi klass exceptioni.'''
def __init__(self, length, atleast):
Exception.__init__(self)
self.length = length
self.atleast = atleast
try:
text = input("Nimadir kiriting --> ")
if len(text) < 3:
raise ShortInputException(len(text), 3)
# Bu yerda kerakli amallar bajarilishi mumkin.
except EOFError:
print("Nega EOF qildingiz?")
except ShortInputException as ex:
print("ShortInputException: Kiritilgan satr uzunligi -- {0}; minimum {1} kutilgan edi".format(ex.length, ex.atleast))
Do'stlaringiz bilan baham: |