Author Dusty Phillips Reviewers


else: print("This code called if there is no exception") finally



Download 2,95 Mb.
Pdf ko'rish
bet109/183
Sana20.07.2022
Hajmi2,95 Mb.
#831085
1   ...   105   106   107   108   109   110   111   112   ...   183
Bog'liq
python3-oop

else:
print("This code called if there is no exception")
finally:
print("This cleanup code is always called")
www.it-ebooks.info


Chapter 4
[
 107 
]
If we run this example—which illustrates almost every conceivable exception 
handling scenario—a few times, we'll get different output each time, depending
on which exception 
random
chooses. Here are some example runs:
$ python finally_and_else.py
raising None
This code called if there is no exception
This cleanup code is always called
$ python finally_and_else.py
raising
Caught a TypeError
This cleanup code is always called
$ python finally_and_else.py
raising
Caught some other error: IndexError
This cleanup code is always called
$ python finally_and_else.py
raising
Caught a ValueError
This cleanup code is always called
Note how the 
print
statement in the 
finally
clause is executed no matter what 
happens. This is extremely useful when we need to perform certain tasks after 
our code has finished running (even if an exception has occurred). Some common 
examples include:
• 
Cleaning up an open database connection
• 
Closing an open file
• 
Sending a closing handshake over the network
The 
finally
clause is also very important when we execute a 
return
statement 
from inside a 
try
clause. The 
finally
handle will still be executed before the
value is returned.
www.it-ebooks.info


Expecting the Unexpected

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   105   106   107   108   109   110   111   112   ...   183




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish