0% found this document useful (0 votes)
6 views22 pages

UNIT 4-NEW

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 22

UNIT 4

Exception & Error Handling in Python


Errors and exceptions can lead to program failure or unexpected behavior,
and Python comes with a robust set of tools to improve the stability of the
code.
Errors and exceptions can lead to unexpected behavior or even stop a program from
executing. Python provides various functions and mechanisms to handle these issues
and improve the robustness of the code. In this tutorial, we will learn about various error
types and learn about built-in functions with examples.

An error is an issue in a program that prevents the program from completing its task. In
comparison, an exception is a condition that interrupts the normal flow of the program.
Both errors and exceptions are a type of runtime error, which means they occur during
the execution of a program.

In simple words, the error is a critical issue that a normal application should not catch,
while an exception is a condition that a program should catch.

Let’s learn more about errors and exceptions by looking at various examples.

Errors in Python
Here is an example of a syntax error where a return outside the function means nothing.
We should not handle errors in a program. Instead, we must create a function that
returns the string.

EXAMPLE

return "DataCamp"

OUTPUT

Input In [1]

return "DataCamp"
^

SyntaxError: 'return' outside function

EXAMPLE

def fun():

return "DataCamp"

OUTPUT

Input In [2]

return "DataCamp"

IndentationError: expected an indented block

Exceptions in Python
Exceptions are raised when the program is syntactically correct, but the code
results in an error. This error does not stop the execution of the program,
however, it changes the normal flow of the program.

We have encountered a ZeroDivisionError (Exception). We can handle it at runtime by


using `try` and `except` blocks.

EXAMPLE

test = 1/0

OUTPUT

---------------------------------------------------------------------------

ZeroDivisionError Traceback (most recent call last)

Input In [4], in <cell line: 1>()

----> 1 test = 1/0


ZeroDivisionError: division by zero

Different types of exceptions in python:

 SyntaxError: This exception is raised when the interpreter encounters a


syntax error in the code, such as a misspelled keyword, a missing colon, or
an unbalanced parenthesis.
 TypeError: This exception is raised when an operation or function is applied
to an object of the wrong type, such as adding a string to an integer.
 NameError: This exception is raised when a variable or function name is not
found in the current scope.
 IndexError: This exception is raised when an index is out of range for a list,
tuple, or other sequence types.
 KeyError: This exception is raised when a key is not found in a dictionary.
 ValueError: This exception is raised when a function or method is called
with an invalid argument or input, such as trying to convert a string to an
integer when the string does not represent a valid integer.
 AttributeError: This exception is raised when an attribute or method is not
found on an object, such as trying to access a non-existent attribute of a
class instance.
 IOError: This exception is raised when an I/O operation, such as reading or
writing a file, fails due to an input/output error.
 ZeroDivisionError: This exception is raised when an attempt is made to
divide a number by zero.
 ImportError: This exception is raised when an import statement fails to find
or load a module.
These are just a few examples of the many types of exceptions that can occur
in Python. It’s important to handle exceptions properly in your code using try-
except blocks or other error-handling techniques, in order to gracefully handle
errors and prevent the program from crashing.

RUN TIME ERRORS :

Runtime Errors:
 A runtime error in a program is an error that occurs while the program is
running after being successfully compiled.
 Runtime errors are commonly called referred to as “bugs” and are often
found during the debugging process before the software is released.
There are a variety of runtime errors that occur such as logical
errors, Input/Output errors, undefined object errors, division by zero
errors, and many more.

EXAMPLE :

# Python program to illustrate

# the ZeroDivisionError

# Driver Code

def main():

a=5

try:

# Division by Zero

print(a / 0)

except ZeroDivisionError as e:

print(f"Error: {e}")

main()

OUTPUT :

Output:

Example 2:
x=5

y = "hello"

try:

z=x+y

except TypeError:
print("Error: cannot add an int and a str")

Output
Error: cannot add an int and a str

Ways to avoid Runtime Errors :


 Avoid using variables that have not been initialized. These may be set
to 0 on your system but not on the coding platform.
 Check every single occurrence of an array element and ensure that it is not
out of bounds.
 Avoid declaring too much memory. Check for the memory limit specified in
the question.
 Avoid declaring too much Stack Memory. Large arrays should be declared
globally outside the function.
 Use return as the end statement.
 Avoid referencing free memory or null pointers.

Python Exception Handling Mechanism(Python Exception


Model)

Try and Except Statement – Catching Exceptions


Try and except statements are used to catch and handle exceptions in
Python. Statements that can raise exceptions are kept inside the try clause and
the statements that handle the exception are written inside except clause.
Example: Here we are trying to access the array element whose index is out of
bound and handle the corresponding exception.
 Python3

a = [1, 2, 3]

try:

print ("Second element = %d" %(a[1]))


print ("Fourth element = %d" %(a[3]))

except:

print ("An error occurred")

Output

Second element = 2
An error occurred
Catching Specific Exception
A try statement can have more than one except clause, to specify handlers for
different exceptions. Please note that at most one handler will be executed. For
example, we can add IndexError in the above code. The general syntax for
adding specific exceptions are –
try:
# statement(s)
except IndexError:
# statement(s)
except ValueError:
# statement(s)
Example: Catching specific exceptions in the Python
The code defines a function ‘fun(a)' that calculates b based on the input a.
If a is less than 4, it attempts a division by zero, causing
a ‘ZeroDivisionError'. The code calls fun(3) and fun(5) inside a try-except
block. It handles the ZeroDivisionError for fun(3) and
prints “ZeroDivisionError Occurred and Handled.” The ‘NameError' block is
not executed since there are no ‘NameError' exceptions in the code.
 Python3
def fun(a):

if a < 4:
b = a/(a-3)

print("Value of b = ", b)

try:

fun(3)

fun(5)

except ZeroDivisionError:

print("ZeroDivisionError Occurred and Handled")

except NameError:

print("NameError Occurred and Handled")

Output
ZeroDivisionError Occurred and Handled
If you comment on the line fun(3), the output will be
NameError Occurred and Handled
The output above is so because as soon as python tries to access the value of
b, NameError occurs.
Try with Else Clause
In Python, you can also use the else clause on the try-except block which must
be present after all the except clauses. The code enters the else block only if
the try clause does not raise an exception.
Try with else clause
The code defines a function AbyB(a, b) that calculates c as ((a+b) / (a-b)) and
handles a potential ZeroDivisionError. It prints the result if there’s no division
by zero error. Calling AbyB(2.0, 3.0) calculates and prints -5.0, while
calling AbyB(3.0, 3.0) attempts to divide by zero, resulting in
a ZeroDivisionError, which is caught and “a/b results in 0” is printed.
 Python3
def AbyB(a , b):

try:

c = ((a+b) / (a-b))

except ZeroDivisionError:
print ("a/b result in 0")

else:

print (c)

AbyB(2.0, 3.0)

AbyB(3.0, 3.0)

Output:
-5.0
a/b result in 0
Finally Keyword in Python
Python provides a keyword finally, which is always executed after the try and
except blocks. The final block always executes after the normal termination of
the try block or after the try block terminates due to some exception.
Syntax:
try:
# Some Code....

except:
# optional block
# Handling of exception (if required)

else:
# execute if no exception

finally:
# Some code .....(always executed)
Example:
The code attempts to perform integer division by zero, resulting in
a ZeroDivisionError. It catches the exception and prints “Can’t divide by
zero.” Regardless of the exception, the finally block is executed and
prints “This is always executed.”

try:

k = 5//0

print(k)

except ZeroDivisionError:
print("Can't divide by zero")

finally:

print('This is always executed')

Output:
Can't divide by zero
This is always executed
Raising Exception
The raise statement allows the programmer to force a specific exception to
occur. The sole argument in raise indicates the exception to be raised. This
must be either an exception instance or an exception class (a class that derives
from Exception).
This code intentionally raises a NameError with the message “Hi there” using
the raise statement within a try block. Then, it catches
the NameError exception, prints “An exception,” and re-raises the same
exception using raise. This demonstrates how exceptions can be raised and
handled in Python, allowing for custom error messages and further exception
propagation.
Example :
try:

raise NameError("Hi there")

except NameError:

print ("An exception")

raise

The output of the above code will simply line printed as “An exception” but a
Runtime error will also occur in the last due to the raise statement in the last
line. So, the output on your command line will look like
Traceback (most recent call last):
File "/home/d6ec14ca595b97bff8d8034bbf212a9f.py", line 5, in
<module>
raise NameError("Hi there") # Raise Error
NameError: Hi there
Exception Hierarchy
In Python, exceptions are a way to handle errors and other
exceptional events that may occur during program execution.
Python’s exception hierarchy provides a well-organized way
to categorize and handle different types of exceptions. The
defined exception structure also provides a deterministic way
of handling different types of exceptions with except blocks.

The Exception Hierarchy


In Python, all exceptions are derived from
the BaseException class. This means that every exception in
Python is an instance of BaseException or one of its subclasses.

The BaseException class is the top-level class in the exception


hierarchy. It provides some common methods that all
exceptions can use, such as __str__ and __repr__. However,
you should not use BaseException directly in your code as it is
too broad and can catch any type of exception.

Instead, you should use more specific exception classes that


are subclasses of BaseException. Python provides a number of
built-in exception classes that you can use, and you can also
create your own custom exception classes.
The following diagram shows the hierarchy of built-in
exception classes in Python (taken from the official Python
documentation):

BaseException
├── BaseExceptionGroup
├── GeneratorExit
├── KeyboardInterrupt
├── SystemExit
└── Exception
├── ArithmeticError
│ ├── FloatingPointError
│ ├── OverflowError
│ └── ZeroDivisionError
├── AssertionError
├── AttributeError
├── BufferError
├── EOFError
├── ExceptionGroup [BaseExceptionGroup]
├── ImportError
│ └── ModuleNotFoundError
├── LookupError
│ ├── IndexError
│ └── KeyError
├── MemoryError
├── NameError
│ └── UnboundLocalError
├── OSError
│ ├── BlockingIOError
│ ├── ChildProcessError
│ ├── ConnectionError
│ │ ├── BrokenPipeError
│ │ ├── ConnectionAbortedError
│ │ ├── ConnectionRefusedError
│ │ └── ConnectionResetError
│ ├── FileExistsError
│ ├── FileNotFoundError
│ ├── InterruptedError
│ ├── IsADirectoryError
│ ├── NotADirectoryError
│ ├── PermissionError
│ ├── ProcessLookupError
│ └── TimeoutError
├── ReferenceError
├── RuntimeError
│ ├── NotImplementedError
│ └── RecursionError
├── StopAsyncIteration
├── StopIteration
├── SyntaxError
│ └── IndentationError
│ └── TabError
├── SystemError
├── TypeError
├── ValueError
│ └── UnicodeError
│ ├── UnicodeDecodeError
│ ├── UnicodeEncodeError
│ └── UnicodeTranslateError
└── Warning
├── BytesWarning
├── DeprecationWarning
├── EncodingWarning
├── FutureWarning
├── ImportWarning
├── PendingDeprecationWarning
├── ResourceWarning
├── RuntimeWarning
├── SyntaxWarning
├── UnicodeWarning
└── UserWarning

This means that the FileNotFoundError is a subclass of


an OSError, which is a subclass of the Exception class, which
itself inherits from the BaseException.

Handling multiple exceptions with a


single except statement
To handle multiple exceptions with a single except statement,
you can specify a tuple of exception types to catch, like this:

try:
# some code that may raise an exception
except (ExceptionType1, ExceptionType2):
# handle the exception
In this example, both ExceptionType1 and ExceptionType2 are
exceptions that you want to catch. If the code in the try block
raises an instance of either of these exceptions, the code in
the except block will execute.

Here’s an example with specific exception types:

try:
# some code that may raise an exception
except (TypeError, ValueError):
# handle the exception

In this example, if the code in the try block raises either


a TypeError or a ValueError, the code in the except block will
execute.

Example

try:

n = int(input("Input a number: "))

result = 10 / n

except (ValueError, ZeroDivisionError):

print("Invalid input or division by zero occurred.")


Handling multiple exceptions with
multiple except statements
Alternatively, you can handle multiple exceptions with
multiple except statements. In this case, each except statement
handles a different exception type:

try:
# some code that may raise an exception
except TypeError:
# handle the TypeError
except ValueError:
# handle the ValueError

In this example, if the code in the try block raises a TypeError,

the code in the first except block will execute. If the code
raises a ValueError, the code in the second except block will
execute.

Example:

try:

f = open('missing')

except OSError:

print('It failed')
except FileNotFoundError:

print('File not found')


Two types of files can be handled in Python , normal text
files and binary files (written in binary language, 0s, and 1s).
 Text files: In this type of file, Each line of text is terminated with a special
character called EOL (End of Line), which is the new line character (‘\n’) in
Python by default.
 Binary files: In this type of file, there is no terminator for a line, and the data
is stored after converting it into machine-understandable binary language.

File Access Modes


Access modes govern the type of operations possible in the opened file. It
refers to how the file will be used once its opened. These modes also define the
location of the File Handle in the file. The file handle is like a cursor, which
defines from where the data has to be read or written in the file and we can get
Python output in text file.
There are 6 access modes in Python:
 Read Only (‘r’)
 Read and Write (‘r+’)
 Write Only (‘w’)
 Write and Read (‘w+’)
 Append Only (‘a’)
 Append and Read (‘a+’)
Read Only (‘r’) : Open text file for reading. The handle is positioned at the
beginning of the file. If the file does not exist, raises the I/O error. This is also
the default mode in which a file is opened.
Read and Write (‘r+’): Open the file for reading and writing. The handle is
positioned at the beginning of the file. Raises I/O error if the file does not exist.
Write Only (‘w’) : Open the file for writing. For the existing files, the data is
truncated and over-written. The handle is positioned at the beginning of the file.
Creates the file if the file does not exist.
Write and Read (‘w+’) : Open the file for reading and writing. For an existing file,
data is truncated and over-written. The handle is positioned at the beginning of
the file.
Append Only (‘a’): Open the file for writing. The file is created if it does not exist.
The handle is positioned at the end of the file. The data being written will be
inserted at the end, after the existing data.
Append and Read (‘a+’) : Open the file for reading and writing. The file is
created if it does not exist. The handle is positioned at the end of the file. The
data being written will be inserted at the end, after the existing data.

# Open function to open the file "MyFile1.txt"

# (same directory) in append mode and

file1 = open("MyFile1.txt","a")

# store its reference in the variable file1

# and "MyFile2.txt" in D:\Text in file2


file2 = open("D:\Text\MyFile2.txt","w+")
How to Create Files in Python
In Python, you use the open() function with one of the following
options – "x" or "w" – to create a new file:
 "x" – Create: this command will create a new file if and
only if there is no file already in existence with that
name or else it will return an error.
Example of creating a file in Python using the "x" command:

#creating a text file with the command function "x"

f = open("myfile.txt", "x")
We've now created a new empty text file! But if you retry the
code above – for example, if you try to create a new file with the
same name as you used above (if you want to reuse the
filename above) you will get an error notifying you that the file
already exists. It'll look like the image below:
 "w" – Write: this command will create a new text file
whether or not there is a file in the memory with the
new specified name. It does not return an error if it
finds an existing file with the same name – instead it
will overwrite the existing file.
Example of how to create a file with the "w" command:

#creating a text file with the command function "w"

f = open("myfile.txt", "w")

#This "w" command can also be used create a new file but unlike the the "x" command
the "w" command will overwrite any existing file found with the same file name.
With the code above, whether the file exists or the file doesn't
exist in the memory, you can still go ahead and use that code.
Just keep in mind that it will overwrite the file if it finds an
existing file with the same name.
How to Write to a File in Python
There are two methods of writing to a file in Python, which are:
The write() method:
This function inserts the string into the text file on a single line.

Based on the file we have created above, the below line of code
will insert the string into the created text file, which is
"myfile.txt.”

file.write("Hello There\n")
The writelines() method:
This function inserts multiple strings at the same time. A list of
string elements is created, and each string is then added to the
text file.

Using the previously created file above, the below line of code
will insert the string into the created text file, which is
"myfile.txt.”

f.writelines(["Hello World ", "You are welcome to Fcc\n"])


Example:

#This program shows how to write data in a text file.

file = open("myfile.txt","w")
L = ["This is Lagos \n","This is Python \n","This is Fcc \n"]

# i assigned ["This is Lagos \n","This is Python \n","This is Fcc \n"] to #variable L, you
can use any letter or word of your choice.
# Variable are containers in which values can be stored.
# The \n is placed to indicate the end of the line.

file.write("Hello There \n")


file.writelines(L)
file.close()

Reading from a file in Python


There are three ways to read data from a text file:
 Using read()
 Using readline()
 Using readlines()
Reading From a File Using read()
read() : Returns the read bytes in form of a string. Reads n bytes, if no n
specified, reads the entire file.
File_object.read([n])
Reading a Text File Using readline()
readline() : Reads a line of the file and returns in form of a string.For specified n,
reads at most n bytes. However, does not reads more than one line, even if n
exceeds the length of the line.
File_object.readline([n])
Reading a File Using readlines()
readlines() : Reads all the lines and return them as each line a string element in
a list.
File_object.readlines()
Note: ‘\n’ is treated as a special character of two bytes.
In this example, a file named “myfile.txt” is created and opened in write mode
("w"). Data is written to the file using write and writelines methods. The file is
then reopened in read and append mode ( "r+"). Various read operations,
including read, readline, readlines, and the use of seek, demonstrate different
ways to retrieve data from the file. Finally, the file is closed.
 Python3

# Program to show various ways to read and

# write data in a file.

file1 = open("myfile.txt", "w")

L = ["This is Delhi \n", "This is Paris \


n", "This is London \n"]
# \n is placed to indicate EOL (End of
Line)

file1.write("Hello \n")

file1.writelines(L)

file1.close() # to change file access


modes

file1 = open("myfile.txt", "r+")

print("Output of Read function is ")

print(file1.read())

print()

# seek(n) takes the file handle to the nth

# byte from the beginning.

file1.seek(0)

print("Output of Readline function is ")


print(file1.readline())

print()

file1.seek(0)

# To show difference between read and


readline

print("Output of Read(9) function is ")

print(file1.read(9))

print()

file1.seek(0)

print("Output of Readline(9) function is


")

print(file1.readline(9))

file1.seek(0)

# readlines function
print("Output of Readlines function is ")

print(file1.readlines())

print()

file1.close()

Output:
Output of Read function is
Hello
This is Delhi
This is Paris
This is London
Output of Readline function is
Hello
Output of Read(9) function is
Hello
Th
Output of Readline(9) function is
Hello
Output of Readlines function is
['Hello \n', 'This is Delhi \n', 'This is Paris \n', 'This is
London \n']

You might also like