Author Dusty Phillips Reviewers


raise ValueError("Only even numbers can be added")



Download 2,95 Mb.
Pdf ko'rish
bet103/183
Sana20.07.2022
Hajmi2,95 Mb.
#831085
1   ...   99   100   101   102   103   104   105   106   ...   183
Bog'liq
python3-oop

raise ValueError("Only even numbers can be added")
ValueError: Only even numbers can be added
>>> e.append(2)
www.it-ebooks.info


Chapter 4
[
 101 
]
While this class is effective for demonstrating exceptions in action, 
it isn't very good at its job. It is still possible to get other values 
into the list using index notation or slice notation. This can all be 
avoided by overriding other appropriate methods, some of which 
are double-underscore methods.
The effects of an exception
When an exception is raised, it appears to stop program execution immediately.
Any lines that were supposed to run after the exception is raised are not executed, 
and unless the exception is dealt with, the program will exit with an error message. 
Take a look at this simple function:
def no_return():
print("I am about to raise an exception")
raise Exception("This is always raised")
print("This line will never execute")
return "I won't be returned"
If we execute this function, we see that the first 
print
call is executed and then the 
exception is raised. The second 
print
statement is never executed, and the 
return
statement never executes either:
>>> no_return()
I am about to raise an exception
Traceback (most recent call last):
File "", line 1, in
File "exception_quits.py", line 3, in no_return
raise Exception("This is always raised")
Exception: This is always raised
Furthermore, if we have a function that calls another function that raises an exception, 
nothing will be executed in the first function after the point where the second function 
was called. Raising an exception stops all execution right up through the function call 
stack until it is either handled or forces the interpreter to exit. To demonstrate, let's add 
a second function that calls the earlier one:
def call_exceptor():
print("call_exceptor starts here...")
no_return()
print("an exception was raised...")
print("...so these lines don't run")
www.it-ebooks.info


Expecting the Unexpected

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   99   100   101   102   103   104   105   106   ...   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