0% found this document useful (0 votes)
15 views

B13_TuHoc_09_Exception Handling

Uploaded by

nthang0987
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

B13_TuHoc_09_Exception Handling

Uploaded by

nthang0987
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Python Exceptions

Lê Ngọc Hiếu
[email protected]
Introduction to Python exceptions
• A Python program terminates as soon as it encounters an
error.
• In Python, an error can be a syntax error or an exception
error.

2
Catching Exceptions in Python
• In Python, exceptions can be handled using a try and
exception statement. try:
• Syntax: # statements
.....
• The try block: use to test a block
except
of code for errors.
ExceptionErrorName:
• The except block: use to handle
# statements
the error.
.....
• The else block: defines a block of
else:
code to be executed if no errors
# statements
were raised (This block is
.....
optional).
finally:
• The finally block: to execute
# statements
code, regardless of the result of
..... 3
Catching Exceptions in Python: Example

4
Catching Exceptions in Python: Example

5
Python Built-in Exceptions
Exception Description
ArithmeticError Raised when an error occurs in numeric calculations
AssertionError Raised when an assert statement fails
AttributeError Raised when attribute reference or assignment fails
Exception Base class for all exceptions

Raised when the input() method hits an "end of file"


EOFError
condition (EOF)

FloatingPointError Raised when a floating point calculation fails


Raised when a generator is closed (with the close()
GeneratorExit
method) 6
Python Built-in Exceptions
Exception Description
ImportError Raised when an imported module does not exist
IndentationError Raised when indendation is not correct
IndexError Raised when an index of a sequence does not exist
KeyError Raised when a key does not exist in a dictionary
KeyboardInterrupt Raised when the user presses Ctrl+c, Ctrl+z or Delete
LookupError Raised when errors raised cannot be found
MemoryError Raised when a program runs out of memory
NameError Raised when a variable does not exist
NotImplementedEr Raised when an abstract method requires an inherited
ror class to override the method 7
Python Built-in Exceptions
Exception Description
OSError Raised when a system related operation causes an error
OverflowError Raised when the result of a numeric calculation is too
large
ReferenceError Raised when a weak reference object does not exist
RuntimeError Raised when an error occurs that do not belong to any
specific expections
StopIteration Raised when the next() method of an iterator has no
further values
SyntaxError Raised when a syntax error occurs
TabError Raised when indentation consists of tabs or spaces
SystemError Raised when a system error occurs
SystemExit Raised when the sys.exit() function is called 8
Python Built-in Exceptions
Exception Description
TypeError Raised when two different types are combined
Raised when a local variable is referenced before
UnboundLocalError
assignment
UnicodeError Raised when a unicode problem occurs
UnicodeEncodeError Raised when a unicode encoding problem occurs

UnicodeDecodeError Raised when a unicode decoding problem occurs

UnicodeTranslateErro
Raised when a unicode translation problem occurs
r
Raised when there is a wrong value in a specified
ValueError
data type
ZeroDivisionError Raised when the second operator in a division is zero 9
Many Exceptions
• You can define many
exception blocks.
• Each block of code for a
special kind of error.

10
Many Exceptions

11
Raise an Exception
• We can throw an exception if a condition occurs.
• To throw (or raise) an exception, use the raise keyword.

12
User-Defined Exception

13

You might also like