0% found this document useful (0 votes)
1 views14 pages

ML_Exceptions

The document explains exceptions in Python, detailing their two phases: the occurrence of an error and the handling of that error. It describes how exceptions are raised and provides examples of common exceptions like NameError, ZeroDivisionError, and IndexError. Additionally, it outlines the use of try-except and try-finally statements for detecting and managing exceptions in code.

Uploaded by

riaz ahamed
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)
1 views14 pages

ML_Exceptions

The document explains exceptions in Python, detailing their two phases: the occurrence of an error and the handling of that error. It describes how exceptions are raised and provides examples of common exceptions like NameError, ZeroDivisionError, and IndexError. Additionally, it outlines the use of try-except and try-finally statements for detecting and managing exceptions in code.

Uploaded by

riaz ahamed
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/ 14

MACHINE LEARNING WITH

PYTHON

EXCEPTIONS
• What Are Exceptions?

• Exceptions

• Exceptions can best be described as action that


• is taken outside of the normal flow of control
• because of errors. This action comes in two
• distinct phases, the first being the error which
• causes an exception to occur, and the second
• being the detection (and possible resolution)
• phase.
• What Are Exceptions?

– The first phase takes place when an exception
condition (sometimes referred to as
exceptional condition) occurs.
– Upon detection of an error and recognition of
the exception condition, the interpreter
performs an operation called raising an
exception.
– Raising is also known as triggering, throwing,
or generating, and is the process whereby the
interpreter makes it known to the current
control flow that something is wrong.
– Python also supports the ability of the
programmer's to raise exceptions.
• What Are Exceptions?

The second phase is where exception handling
takes place. Once an exception is raised, a
variety of actions can be invoked in response to
that exception.
These can range anywhere from ignoring the
error, logging the error but otherwise taking no
action, performing some corrective measures
and aborting the program, or alleviating the
problem to allow for resumption of execution.
Traceback

A "traceback" notice appears along with a notice


with much diagnostic information such as the
error name, reason, and perhaps even the line
number near or exactly where the error occurred.
NameError: attempt to access an undeclared
Variable
>>> foo
Traceback (innermost last):
File "<interactive input>", line 0, in ?
NameError: foo
NameError indicates access to an uninitialized
variable.
SyntaxError: Python interpreter syntax error
>>> for
File "<string>", line 1
for
^
SyntaxError: invalid syntax
ZeroDivisionError: division by any numeric
zero
>>> 12.4/0.0
Traceback (innermost last):
File "<interactive input>", line 0, in ?
ZeroDivisionError: float division

IndexError: request for an out-of-range


index for sequence
>>> aList = []
>>> aList[0]
Traceback (innermost last):
File "<stdin>", line 1, in ?
IndexError: list index out of range
KeyError: request for a non-existent
dictionary key
>>> aDict = {'host': 'earth', 'port': 80}
>>> print aDict['server']
Traceback (innermost last):
File "<stdin>", line 1, in ?
KeyError: server

IOError: input/output error


>>> f = open("blah")
Traceback (innermost last):
File "<interactive input>", line 1, in ?
IOError: [Errno 2] No such file or directory: 'blah'
Detecting and Handling Exceptions

Exceptions can be detected by incorporating


them as part of a try statement.

Any code suite of a try statement will be


monitored for exceptions.

There are two main forms of the try statement:


try-except and try-finally.

A try statement is either accompanied by


one or more except clauses or exactly one
finally clause.
try-except Statement

The try-except statement allows to define a


section of code to monitor for exceptions and
also provides the mechanism to execute handlers
for exceptions.

Syntax :
try:
try_suite # watch for exceptions here
except Exception:
except_suite # exception-handling code
try-except Statement – Example

>>> try:
… f = open('blah')
… except IOError:
… print 'could not open file'

could not open file
try-except Statement

During run-time, the interpreter attempts to


execute all the code within the try statement.

If an exception does not occur when the


code block has completed, execution resumes
past the except statement.

When the specified exception named on the


except statement does occur, control flow
immediately continues in the handler (all
remaining code in the try clause is skipped).
try-finally Statement

The try-finally statement differs from its try-


except brethren in that it is not used to
handle exceptions.

Instead it is used to maintain consistent


behavior regardless of whether or not exceptions
occur.

The finally suite executes regardless of an


exception being triggered within the try suite.
try-finally Statement

try:
try_suite
finally:
finally_suite # executes regardless of
exceptions

You might also like