Author Dusty Phillips Reviewers



Download 2,95 Mb.
Pdf ko'rish
bet112/183
Sana20.07.2022
Hajmi2,95 Mb.
#831085
1   ...   108   109   110   111   112   113   114   115   ...   183
Bog'liq
python3-oop

[
 110 
]
Of course, if we do want to customize the initializer, we are free to do so. Here's 
an exception whose initializer accepts the current balance and the amount the user 
wanted to withdraw. In addition, it adds a method to calculate how overdrawn the 
request was:
class InvalidWithdrawal(Exception):
def __init__(self, balance, amount):
super().__init__("account doesn't have ${}".format(
amount))
self.amount = amount
self.balance = balance
def overage(self):
return self.amount - self.balance
raise InvalidWithdrawal(25, 50)
The 
raise
statement at the end illustrates how to construct this exception. As you 
can see, we can do anything with an exception that we would do with other objects. 
We could catch an exception and pass it around as a working object, although it 
is more common to include a reference to the working object as an attribute on an 
exception and pass that around instead.
Here's how we would handle an 
InvalidWithdrawal
exception if one was raised:
try:
raise InvalidWithdrawal(25, 50)
except InvalidWithdrawal as e:
print("I'm sorry, but your withdrawal is "
"more than your balance by "
"${}".format(e.overage()))
Here we see a valid use of the 
as
keyword. By convention, most Python coders
name the exception variable 
e
, although, as usual, you are free to call it 
ex

exception
, or 
aunt_sally
if you prefer.
There are many reasons for defining our own exceptions. It is often useful to add 
information to the exception or log it in some way. But the utility of custom exceptions 
truly comes to light when creating a framework, library, or API that is intended for 
access by other programmers. In that case, be careful to ensure your code is raising 
exceptions that make sense to the client programmer. They should be easy to handle 
and clearly describe what went on. The client programmer should easily see how to
fix the error (if it reflects a bug in their code) or handle the exception (if it's a situation 
they need to be made aware of).
www.it-ebooks.info


Chapter 4

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   108   109   110   111   112   113   114   115   ...   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