0% found this document useful (0 votes)
96 views29 pages

MC4103 Python Programming - Unit-Iii

MC4103 PYTHON PROGRAMMING UNIT-III

Uploaded by

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

MC4103 Python Programming - Unit-Iii

MC4103 PYTHON PROGRAMMING UNIT-III

Uploaded by

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

UNIT III FILE HANDLING AND EXCEPTION HANDLING 8

Files: Introduction – File Path – Opening and Closing Files – Reading and Writing Files –File
Position –Exception: Errors and Exceptions, Exception Handling, Multiple Exceptions
Python - Files I/O

This chapter covers all the basic I/O functions available in Python. For more functions, please
refer to standard Python documentation.

Printing to the Screen

The simplest way to produce output is using the print statement where you can pass zero or more
expressions separated by commas. This function converts the expressions you pass into a string
and writes the result to standard output as follows −

#!/usr/bin/python

print "Python is really a great language,", "isn't it?"

This produces the following result on your standard screen −

Python is really a great language, isn't it?

Reading Keyboard Input

Python provides two built-in functions to read a line of text from standard input, which by
default comes from the keyboard. These functions are −

 raw_input
 input

The raw_input Function

The raw_input([prompt]) function reads one line from standard input and returns it as a string
(removing the trailing newline).

#!/usr/bin/python

str = raw_input("Enter your input: ")


print "Received input is : ", str

This prompts you to enter any string and it would display same string on the screen. When I
typed "Hello Python!", its output is like this −

Enter your input: Hello Python


Received input is : Hello Python
The input Function

The input([prompt]) function is equivalent to raw_input, except that it assumes the input is a
valid Python expression and returns the evaluated result to you.

#!/usr/bin/python

str = input("Enter your input: ")


print "Received input is : ", str

This would produce the following result against the entered input −

Enter your input: [x*5 for x in range(2,10,2)]


Recieved input is : [10, 20, 30, 40]

Opening and Closing Files

Until now, you have been reading and writing to the standard input and output. Now, we will see
how to use actual data files.

Python provides basic functions and methods necessary to manipulate files by default. You can
do most of the file manipulation using a file object.

The open Function

Before you can read or write a file, you have to open it using Python's built-in open() function.
This function creates a file object, which would be utilized to call other support methods
associated with it.

Syntax

file object = open(file_name [, access_mode][, buffering])

Here are parameter details −

 file_name − The file_name argument is a string value that contains the name of the file
that you want to access.
 access_mode − The access_mode determines the mode in which the file has to be
opened, i.e., read, write, append, etc. A complete list of possible values is given below in
the table. This is optional parameter and the default file access mode is read (r).
 buffering − If the buffering value is set to 0, no buffering takes place. If the buffering
value is 1, line buffering is performed while accessing a file. If you specify the buffering
value as an integer greater than 1, then buffering action is performed with the indicated
buffer size. If negative, the buffer size is the system default(default behavior).

Here is a list of the different modes of opening a file −


Sr.No. Modes & Description
r
1
Opens a file for reading only. The file pointer is placed at the beginning of the file. This
is the default mode.
rb
2
Opens a file for reading only in binary format. The file pointer is placed at the beginning
of the file. This is the default mode.
r+
3
Opens a file for both reading and writing. The file pointer placed at the beginning of the
file.
rb+
4
Opens a file for both reading and writing in binary format. The file pointer placed at the
beginning of the file.
w
5
Opens a file for writing only. Overwrites the file if the file exists. If the file does not
exist, creates a new file for writing.
wb
6
Opens a file for writing only in binary format. Overwrites the file if the file exists. If the
file does not exist, creates a new file for writing.
w+
7
Opens a file for both writing and reading. Overwrites the existing file if the file exists. If
the file does not exist, creates a new file for reading and writing.
wb+
8
Opens a file for both writing and reading in binary format. Overwrites the existing file if
the file exists. If the file does not exist, creates a new file for reading and writing.
a

9 Opens a file for appending. The file pointer is at the end of the file if the file exists. That
is, the file is in the append mode. If the file does not exist, it creates a new file for
writing.
ab

10 Opens a file for appending in binary format. The file pointer is at the end of the file if the
file exists. That is, the file is in the append mode. If the file does not exist, it creates a
new file for writing.
11 a+
Opens a file for both appending and reading. The file pointer is at the end of the file if
the file exists. The file opens in the append mode. If the file does not exist, it creates a
new file for reading and writing.
ab+

12 Opens a file for both appending and reading in binary format. The file pointer is at the
end of the file if the file exists. The file opens in the append mode. If the file does not
exist, it creates a new file for reading and writing.

The file Object Attributes

Once a file is opened and you have one file object, you can get various information related to that
file.

Here is a list of all attributes related to file object −

Sr.No. Attribute & Description


file.closed
1
Returns true if file is closed, false otherwise.
file.mode
2
Returns access mode with which file was opened.
file.name
3
Returns name of the file.
file.softspace
4
Returns false if space explicitly required with print, true otherwise.

Example

#!/usr/bin/python

# Open a file
fo = open("foo.txt", "wb")
print "Name of the file: ", fo.name
print "Closed or not : ", fo.closed
print "Opening mode : ", fo.mode
print "Softspace flag : ", fo.softspace

This produces the following result −

Name of the file: foo.txt


Closed or not : False
Opening mode : wb
Softspace flag : 0

The close() Method

The close() method of a file object flushes any unwritten information and closes the file object,
after which no more writing can be done.

Python automatically closes a file when the reference object of a file is reassigned to another file.
It is a good practice to use the close() method to close a file.

Syntax

fileObject.close()

Example

#!/usr/bin/python

# Open a file
fo = open("foo.txt", "wb")
print "Name of the file: ", fo.name

# Close opend file


fo.close()

This produces the following result −

Name of the file: foo.txt

Reading and Writing Files

The file object provides a set of access methods to make our lives easier. We would see how to
use read() and write() methods to read and write files.

The write() Method

The write() method writes any string to an open file. It is important to note that Python strings
can have binary data and not just text.

The write() method does not add a newline character ('\n') to the end of the string −

Syntax

fileObject.write(string)
Here, passed parameter is the content to be written into the opened file.

Example

#!/usr/bin/python

# Open a file
fo = open("foo.txt", "wb")
fo.write( "Python is a great language.\nYeah its great!!\n")

# Close opend file


fo.close()

The above method would create foo.txt file and would write given content in that file and finally
it would close that file. If you would open this file, it would have following content.

Python is a great language.


Yeah its great!!

The read() Method

The read() method reads a string from an open file. It is important to note that Python strings can
have binary data. apart from text data.

Syntax

fileObject.read([count])

Here, passed parameter is the number of bytes to be read from the opened file. This method starts
reading from the beginning of the file and if count is missing, then it tries to read as much as
possible, maybe until the end of file.

Example

Let's take a file foo.txt, which we created above.

#!/usr/bin/python

# Open a file
fo = open("foo.txt", "r+")
str = fo.read(10);
print "Read String is : ", str
# Close opend file
fo.close()

This produces the following result −


Read String is : Python is

File Positions

The tell() method tells you the current position within the file; in other words, the next read or
write will occur at that many bytes from the beginning of the file.

The seek(offset[, from]) method changes the current file position. The offset argument indicates
the number of bytes to be moved. The from argument specifies the reference position from where
the bytes are to be moved.

If from is set to 0, it means use the beginning of the file as the reference position and 1 means use
the current position as the reference position and if it is set to 2 then the end of the file would be
taken as the reference position.

Example

Let us take a file foo.txt, which we created above.

#!/usr/bin/python

# Open a file
fo = open("foo.txt", "r+")
str = fo.read(10)
print "Read String is : ", str

# Check current position


position = fo.tell()
print "Current file position : ", position

# Reposition pointer at the beginning once again


position = fo.seek(0, 0);
str = fo.read(10)
print "Again read String is : ", str
# Close opend file
fo.close()

This produces the following result −

Read String is : Python is


Current file position : 10
Again read String is : Python is

Renaming and Deleting Files


Python os module provides methods that help you perform file-processing operations, such as
renaming and deleting files.

To use this module you need to import it first and then you can call any related functions.

The rename() Method

The rename() method takes two arguments, the current filename and the new filename.

Syntax

os.rename(current_file_name, new_file_name)

Example

Following is the example to rename an existing file test1.txt −

#!/usr/bin/python
import os

# Rename a file from test1.txt to test2.txt


os.rename( "test1.txt", "test2.txt" )

The remove() Method

You can use the remove() method to delete files by supplying the name of the file to be deleted
as the argument.

Syntax

os.remove(file_name)

Example

Following is the example to delete an existing file test2.txt −

#!/usr/bin/python
import os

# Delete file test2.txt


os.remove("text2.txt")

Directories in Python
All files are contained within various directories, and Python has no problem handling these too.
The os module has several methods that help you create, remove, and change directories.

The mkdir() Method

You can use the mkdir() method of the os module to create directories in the current directory.
You need to supply an argument to this method which contains the name of the directory to be
created.

Syntax

os.mkdir("newdir")

Example

Following is the example to create a directory test in the current directory −

#!/usr/bin/python
import os

# Create a directory "test"


os.mkdir("test")

The chdir() Method

You can use the chdir() method to change the current directory. The chdir() method takes an
argument, which is the name of the directory that you want to make the current directory.

Syntax

os.chdir("newdir")

Example

Following is the example to go into "/home/newdir" directory −

#!/usr/bin/python
import os

# Changing a directory to "/home/newdir"


os.chdir("/home/newdir")

The getcwd() Method

The getcwd() method displays the current working directory.


Syntax

os.getcwd()

Example

Following is the example to give current directory −

#!/usr/bin/python
import os

# This would give location of the current directory


os.getcwd()

The rmdir() Method

The rmdir() method deletes the directory, which is passed as an argument in the method.

Before removing a directory, all the contents in it should be removed.

Syntax

os.rmdir('dirname')

Example

Following is the example to remove "/tmp/test" directory. It is required to give fully qualified
name of the directory, otherwise it would search for that directory in the current directory.

#!/usr/bin/python
import os

# This would remove "/tmp/test" directory.


os.rmdir( "/tmp/test" )

File & Directory Related Methods

There are three important sources, which provide a wide range of utility methods to handle and
manipulate files & directories on Windows and Unix operating systems. They are as follows −

 File Object Methods: The file object provides functions to manipulate files.
 OS Object Methods: This provides methods to process files as well as directories.

Python - Error Types


The most common reason of an error in a Python program is when a certain statement is not in
accordance with the prescribed usage. Such an error is called a syntax error. The Python
interpreter immediately reports it, usually along with the reason.

Example: Error
>>> print "hello"
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("hello")?

In Python 3.x, print is a built-in function and requires parentheses. The statement above violates
this usage and hence syntax error is displayed.

Many times though, a program results in an error after it is run even if it doesn't have any syntax
error. Such an error is a runtime error, called an exception. A number of built-in exceptions are
defined in the Python library. Let's see some common error types.

The following table lists important built-in exceptions in Python.

Exception Description
AssertionError Raised when the assert statement fails.
AttributeError Raised on the attribute assignment or reference fails.
EOFError Raised when the input() function hits the end-of-file condition.
FloatingPointError Raised when a floating point operation fails.
GeneratorExit Raised when a generator's close() method is called.
ImportError Raised when the imported module is not found.
IndexError Raised when the index of a sequence is out of range.
KeyError Raised when a key is not found in a dictionary.
KeyboardInterrupt Raised when the user hits the interrupt key (Ctrl+c or delete).
MemoryError Raised when an operation runs out of memory.
NameError Raised when a variable is not found in the local or global scope.
NotImplementedError Raised by abstract methods.
OSError Raised when a system operation causes a system-related error.
Raised when the result of an arithmetic operation is too large to be
OverflowError
represented.
Raised when a weak reference proxy is used to access a garbage
ReferenceError
collected referent.
RuntimeError Raised when an error does not fall under any other category.
Raised by the next() function to indicate that there is no further item to
StopIteration
be returned by the iterator.
SyntaxError Raised by the parser when a syntax error is encountered.
IndentationError Raised when there is an incorrect indentation.
TabError Raised when the indentation consists of inconsistent tabs and spaces.
SystemError Raised when the interpreter detects internal error.
SystemExit Raised by the sys.exit() function.
TypeError Raised when a function or operation is applied to an object of an
Exception Description
incorrect type.
Raised when a reference is made to a local variable in a function or
UnboundLocalError
method, but no value has been bound to that variable.
UnicodeError Raised when a Unicode-related encoding or decoding error occurs.
UnicodeEncodeError Raised when a Unicode-related error occurs during encoding.
UnicodeDecodeError Raised when a Unicode-related error occurs during decoding.
UnicodeTranslateError Raised when a Unicode-related error occurs during translation.
Raised when a function gets an argument of correct type but improper
ValueError
value.
Raised when the second operand of a division or module operation is
ZeroDivisionError
zero.

IndexError

The IndexError is thrown when trying to access an item at an invalid index.

Example: IndexError
>>> L1=[1,2,3]
>>> L1[3]
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>

L1[3]
IndexError: list index out of range

ModuleNotFoundError

The ModuleNotFoundError is thrown when a module could not be found.

Example: ModuleNotFoundError
>>> import notamodule
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>

import notamodule
ModuleNotFoundError: No module named 'notamodule'

KeyError

The KeyError is thrown when a key is not found.

Example: KeyError
>>> D1={'1':"aa", '2':"bb", '3':"cc"}
>>> D1['4']
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>

D1['4']
KeyError: '4'

ImportError

The ImportError is thrown when a specified function can not be found.

Example: ImportError
>>> from math import cube
Traceback (most recent call last):
File "<pyshell#16>", line 1, in <module>

from math import cube


ImportError: cannot import name 'cube'

StopIteration

The StopIteration is thrown when the next() function goes beyond the iterator items.

Example: StopIteration
>>> it=iter([1,2,3])
>>> next(it)
1
>>> next(it)
2
>>> next(it)
3
>>> next(it)
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>

next(it)
StopIteration

TypeError

The TypeError is thrown when an operation or function is applied to an object of an


inappropriate type.

Example: TypeError
>>> '2'+2
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>

'2'+2
TypeError: must be str, not int

ValueError

The ValueError is thrown when a function's argument is of an inappropriate type.

Example: ValueError
>>> int('xyz')
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>

int('xyz')
ValueError: invalid literal for int() with base 10: 'xyz'

NameError

The NameError is thrown when an object could not be found.

Example: NameError
>>> age
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>

age
NameError: name 'age' is not defined

ZeroDivisionError

The ZeroDivisionError is thrown when the second operator in the division is zero.

Example: ZeroDivisionError
>>> x=100/0
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>

x=100/0
ZeroDivisionError: division by zero

KeyboardInterrupt

The KeyboardInterrupt is thrown when the user hits the interrupt key (normally Control-C)
during the execution of the program.
Example: KeyboardInterrupt
>>> name=input('enter your name')
enter your name^c
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>

name=input('enter your name')


KeyboardInterrupt
Python - Exceptions Handling

Python provides two very important features to handle any unexpected error in your Python
programs and to add debugging capabilities in them −

 Exception Handling − This would be covered in this tutorial. Here is a list standard
Exceptions available in Python: Standard Exceptions.
 Assertions − This would be covered in Assertions in Python tutorial.

List of Standard Exceptions −

Sr.No. Exception Name & Description


Exception
1
Base class for all exceptions
StopIteration
2
Raised when the next() method of an iterator does not point to any object.
SystemExit
3
Raised by the sys.exit() function.
StandardError
4
Base class for all built-in exceptions except StopIteration and SystemExit.
ArithmeticError
5
Base class for all errors that occur for numeric calculation.
OverflowError
6
Raised when a calculation exceeds maximum limit for a numeric type.
FloatingPointError
7
Raised when a floating point calculation fails.
ZeroDivisionError
8
Raised when division or modulo by zero takes place for all numeric types.
AssertionError
9
Raised in case of failure of the Assert statement.
AttributeError
10
Raised in case of failure of attribute reference or assignment.
EOFError
11
Raised when there is no input from either the raw_input() or input() function and the end
of file is reached.
ImportError
12
Raised when an import statement fails.
KeyboardInterrupt
13
Raised when the user interrupts program execution, usually by pressing Ctrl+c.
LookupError
14
Base class for all lookup errors.
IndexError
15
Raised when an index is not found in a sequence.
KeyError
16
Raised when the specified key is not found in the dictionary.
NameError
17
Raised when an identifier is not found in the local or global namespace.
UnboundLocalError
18
Raised when trying to access a local variable in a function or method but no value has
been assigned to it.
EnvironmentError
19
Base class for all exceptions that occur outside the Python environment.
IOError
20
Raised when an input/ output operation fails, such as the print statement or the open()
function when trying to open a file that does not exist.
IOError
21
Raised for operating system-related errors.
SyntaxError
22
Raised when there is an error in Python syntax.
IndentationError
23
Raised when indentation is not specified properly.
24 SystemError
Raised when the interpreter finds an internal problem, but when this error is encountered
the Python interpreter does not exit.
SystemExit
25
Raised when Python interpreter is quit by using the sys.exit() function. If not handled in
the code, causes the interpreter to exit.
TypeError
26
Raised when an operation or function is attempted that is invalid for the specified data
type.
ValueError
27
Raised when the built-in function for a data type has the valid type of arguments, but the
arguments have invalid values specified.
RuntimeError
28
Raised when a generated error does not fall into any category.
NotImplementedError
29
Raised when an abstract method that needs to be implemented in an inherited class is not
actually implemented.

Assertions in Python

An assertion is a sanity-check that you can turn on or turn off when you are done with your
testing of the program.

The easiest way to think of an assertion is to liken it to a raise-if statement (or to be more
accurate, a raise-if-not statement). An expression is tested, and if the result comes up false, an
exception is raised.

Assertions are carried out by the assert statement, the newest keyword to Python, introduced in
version 1.5.

Programmers often place assertions at the start of a function to check for valid input, and after a
function call to check for valid output.

The assert Statement

When it encounters an assert statement, Python evaluates the accompanying expression, which is
hopefully true. If the expression is false, Python raises an AssertionError exception.

The syntax for assert is −


assert Expression[, Arguments]

If the assertion fails, Python uses ArgumentExpression as the argument for the AssertionError.
AssertionError exceptions can be caught and handled like any other exception using the try-
except statement, but if not handled, they will terminate the program and produce a traceback.

Example

Here is a function that converts a temperature from degrees Kelvin to degrees Fahrenheit. Since
zero degrees Kelvin is as cold as it gets, the function bails out if it sees a negative temperature −

Live Demo
#!/usr/bin/python
def KelvinToFahrenheit(Temperature):
assert (Temperature >= 0),"Colder than absolute zero!"
return ((Temperature-273)*1.8)+32
print KelvinToFahrenheit(273)
print int(KelvinToFahrenheit(505.78))
print KelvinToFahrenheit(-5)

When the above code is executed, it produces the following result −

32.0
451
Traceback (most recent call last):
File "test.py", line 9, in <module>
print KelvinToFahrenheit(-5)
File "test.py", line 4, in KelvinToFahrenheit
assert (Temperature >= 0),"Colder than absolute zero!"
AssertionError: Colder than absolute zero!

What is Exception?

An exception is an event, which occurs during the execution of a program that disrupts the
normal flow of the program's instructions. In general, when a Python script encounters a
situation that it cannot cope with, it raises an exception. An exception is a Python object that
represents an error.

When a Python script raises an exception, it must either handle the exception immediately
otherwise it terminates and quits.

Handling an exception

If you have some suspicious code that may raise an exception, you can defend your program by
placing the suspicious code in a try: block. After the try: block, include an except: statement,
followed by a block of code which handles the problem as elegantly as possible.
Syntax

Here is simple syntax of try....except...else blocks −

try:
You do your operations here;
......................
except ExceptionI:
If there is ExceptionI, then execute this block.
except ExceptionII:
If there is ExceptionII, then execute this block.
......................
else:
If there is no exception then execute this block.

Here are few important points about the above-mentioned syntax −

 A single try statement can have multiple except statements. This is useful when the try
block contains statements that may throw different types of exceptions.
 You can also provide a generic except clause, which handles any exception.
 After the except clause(s), you can include an else-clause. The code in the else-block
executes if the code in the try: block does not raise an exception.
 The else-block is a good place for code that does not need the try: block's protection.

Example

This example opens a file, writes content in the, file and comes out gracefully because there is no
problem at all −

Live Demo
#!/usr/bin/python

try:
fh = open("testfile", "w")
fh.write("This is my test file for exception handling!!")
except IOError:
print "Error: can\'t find file or read data"
else:
print "Written content in the file successfully"
fh.close()

This produces the following result −

Written content in the file successfully

Example
This example tries to open a file where you do not have write permission, so it raises an
exception −

Live Demo
#!/usr/bin/python

try:
fh = open("testfile", "r")
fh.write("This is my test file for exception handling!!")
except IOError:
print "Error: can\'t find file or read data"
else:
print "Written content in the file successfully"

This produces the following result −

Error: can't find file or read data

The except Clause with No Exceptions

You can also use the except statement with no exceptions defined as follows −

try:
You do your operations here;
......................
except:
If there is any exception, then execute this block.
......................
else:
If there is no exception then execute this block.

This kind of a try-except statement catches all the exceptions that occur. Using this kind of try-
except statement is not considered a good programming practice though, because it catches all
exceptions but does not make the programmer identify the root cause of the problem that may
occur.

The except Clause with Multiple Exceptions

You can also use the same except statement to handle multiple exceptions as follows −

try:
You do your operations here;
......................
except(Exception1[, Exception2[,...ExceptionN]]]):
If there is any exception from the given exception list,
then execute this block.
......................
else:
If there is no exception then execute this block.

The try-finally Clause

You can use a finally: block along with a try: block. The finally block is a place to put any code
that must execute, whether the try-block raised an exception or not. The syntax of the try-finally
statement is this −

try:
You do your operations here;
......................
Due to any exception, this may be skipped.
finally:
This would always be executed.
......................

You cannot use else clause as well along with a finally clause.

Example

Live Demo
#!/usr/bin/python

try:
fh = open("testfile", "w")
fh.write("This is my test file for exception handling!!")
finally:
print "Error: can\'t find file or read data"

If you do not have permission to open the file in writing mode, then this will produce the
following result −

Error: can't find file or read data

Same example can be written more cleanly as follows −

#!/usr/bin/python

try:
fh = open("testfile", "w")
try:
fh.write("This is my test file for exception handling!!")
finally:
print "Going to close the file"
fh.close()
except IOError:
print "Error: can\'t find file or read data"

When an exception is thrown in the try block, the execution immediately passes to the finally
block. After all the statements in the finally block are executed, the exception is raised again and
is handled in the except statements if present in the next higher layer of the try-except statement.

Argument of an Exception

An exception can have an argument, which is a value that gives additional information about the
problem. The contents of the argument vary by exception. You capture an exception's argument
by supplying a variable in the except clause as follows −

try:
You do your operations here;
......................
except ExceptionType, Argument:
You can print value of Argument here...

If you write the code to handle a single exception, you can have a variable follow the name of the
exception in the except statement. If you are trapping multiple exceptions, you can have a
variable follow the tuple of the exception.

This variable receives the value of the exception mostly containing the cause of the exception.
The variable can receive a single value or multiple values in the form of a tuple. This tuple
usually contains the error string, the error number, and an error location.

Example

Following is an example for a single exception −

Live Demo
#!/usr/bin/python

# Define a function here.


def temp_convert(var):
try:
return int(var)
except ValueError, Argument:
print "The argument does not contain numbers\n", Argument

# Call above function here.


temp_convert("xyz");

This produces the following result −


The argument does not contain numbers
invalid literal for int() with base 10: 'xyz'

Raising an Exceptions

You can raise exceptions in several ways by using the raise statement. The general syntax for the
raise statement is as follows.

Syntax

raise [Exception [, args [, traceback]]]

Here, Exception is the type of exception (for example, NameError) and argument is a value for
the exception argument. The argument is optional; if not supplied, the exception argument is
None.

The final argument, traceback, is also optional (and rarely used in practice), and if present, is the
traceback object used for the exception.

Example

An exception can be a string, a class or an object. Most of the exceptions that the Python core
raises are classes, with an argument that is an instance of the class. Defining new exceptions is
quite easy and can be done as follows −

def functionName( level ):


if level < 1:
raise "Invalid level!", level
# The code below to this would not be executed
# if we raise the exception

Note: In order to catch an exception, an "except" clause must refer to the same exception thrown
either class object or simple string. For example, to capture above exception, we must write the
except clause as follows −

try:
Business Logic here...
except "Invalid level!":
Exception handling here...
else:
Rest of the code here...

User-Defined Exceptions

Python also allows you to create your own exceptions by deriving classes from the standard
built-in exceptions.
Here is an example related to RuntimeError. Here, a class is created that is subclassed from
RuntimeError. This is useful when you need to display more specific information when an
exception is caught.

In the try block, the user-defined exception is raised and caught in the except block. The variable
e is used to create an instance of the class Networkerror.

class Networkerror(RuntimeError):
def __init__(self, arg):
self.args = arg

So once you defined above class, you can raise the exception as follows −

try:
raise Networkerror("Bad hostname")
except Networkerror,e:
print e.args
Multiple Exception Handling in Python

 Difficulty Level : Medium


 Last Updated : 12 Jun, 2019

Given a piece of code that can throw any of several different exceptions, and one needs to
account for all of the potential exceptions that could be raised without creating duplicate code or
long, meandering code passages.

If you can handle different exceptions all using a single block of code, they can be grouped
together in a tuple as shown in the code given below :

Code #1 :

try:
client_obj.get_url(url)
except (URLError, ValueError,
SocketTimeout):
client_obj.remove_url(url)

The remove_url() method will be called if any of the listed exceptions occurs. If, on the other
hand, if one of the exceptions has to be handled differently, then put it into its own except clause
as shown in the code given below :

Code #2 :

try:
client_obj.get_url(url)
except (URLError, ValueError):
client_obj.remove_url(url)
except SocketTimeout:
client_obj.handle_url_timeout(url)

Many exceptions are grouped into an inheritance hierarchy. For such exceptions, all of the
exceptions can be caught by simply specifying a base class. For example, instead of writing code
as shown in the code given below –

Code #3 :

try:
f = open(filename)
except (FileNotFoundError, PermissionError):
...
Except statement can be re-written as in the code given below. This works because OSError is a
base class that’s common to both the FileNotFoundError and PermissionError exceptions.

Code #4 :

try:
f = open(filename)
except OSError:
...

Although it’s not specific to handle multiple exceptions per se, it is worth noting that one can get
a handle to the thrown exception using them as a keyword as shown in the code given below.

Code #5 :

try:
f = open(filename)

except OSError as e:
if e.errno == errno.ENOENT:
logger.error('File not found')
elif e.errno == errno.EACCES:
logger.error('Permission denied')
else:
logger.error('Unexpected error: % d',
e.errno)

The e variable holds an instance of the raised OSError. This is useful if the exception has to be
invested further, such as processing it based on the value of the additional status code. The
except clauses are checked in the order listed and the first match executes.

Code #6 : Create situations where multiple except clauses might match

f = open('missing')

Output :

Traceback (most recent call last):


File "", line 1, in
FileNotFoundError: [Errno 2] No such file or directory: 'miss
try:
f = open('missing')
except OSError:
print('It failed')
except FileNotFoundError:
print('File not found')
Output :

Failed

Here the except FileNotFoundError clause doesn’t execute because the OSError is more general,
matches the FileNotFoundError exception, and was listed first.

You might also like