Author Dusty Phillips Reviewers



Download 2,95 Mb.
Pdf ko'rish
bet100/183
Sana20.07.2022
Hajmi2,95 Mb.
#831085
1   ...   96   97   98   99   100   101   102   103   ...   183
Bog'liq
python3-oop

[
 98 
]
Raising exceptions
In principle, an exception is just an object. There are many different exception classes 
available, and we can easily define more of our own. The one thing they all have 
in common is that they inherit from a built-in class called 
BaseException
. These 
exception objects become special when they are handled inside the program's flow of 
control. When an exception occurs, everything that was supposed to happen doesn't 
happen, unless it was supposed to happen when an exception occurred. Make sense? 
Don't worry, it will!
The easiest way to cause an exception to occur is to do something silly! Chances 
are you've done this already and seen the exception output. For example, any time 
Python encounters a line in your program that it can't understand, it bails with 
SyntaxError
, which is a type of exception. Here's a common one:
>>> print "hello world"
File "", line 1
print "hello world"
^
SyntaxError: invalid syntax
This 
print
statement was a valid command in Python 2 and previous versions,
but in Python 3, because 
print
is now a function, we have to enclose the arguments 
in parenthesis. So, if we type the preceding command into a Python 3 interpreter,
we get the 
SyntaxError
.
In addition to 
SyntaxError
, some other common exceptions, which we can handle, 
are shown in the following example:
>>> x = 5 / 0
Traceback (most recent call last):
File "", line 1, in
ZeroDivisionError: int division or modulo by zero
>>> lst = [1,2,3]
>>> print(lst[3])
Traceback (most recent call last):
File "", line 1, in
IndexError: list index out of range

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   96   97   98   99   100   101   102   103   ...   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