filmov
tv
[Python Programming Basics to Advanced]: Exception Handling (try/except/else/finally) | Lab 26

Показать описание
This Python programming playlist is designed to take beginners with zero programming experience to an expert level. The course covers installation, basic syntax, practical scenarios, and efficient logic building. The course material includes PDF handouts, review questions, and covers a wide range of topics, from data types to advanced functions like Lambda and Recursive functions, Generators, and JSON data parsing.
In this lesson we will study about Exception Handling. Exceptions are the errors which will terminate the program execution and as developer we must consider every possible way to handle these errors.
In python we have try and except statements to handle any exception in the program. With try and except, there can be else and finally clauses. We will see the use of these four clauses with different examples.
Then there is also a way to generate an exception, known as raise the exception. We usually need this in Object Oriented Programming. We will use that there but will just see how we can raise an exception.
Moreover, we will see Python and Non-Python way of handling the exception. One is known as Look Before You Leap and second is It's Easy to Ask Forgiveness than Permission.
Complete Playlist:
If you have the basic programming knowledge and interested to learn Object-Oriented Programming in Python, check out this playlist:
Lab Manual 26 can be downloaded from here:
Review Question:
1- Suppose I have a Dictionary with 8 key-value pairs as shown here:
d={
'element1':5,
'element2':2,
'element3':0,
'element4':'Computer',
'element5':3.2,
'element6':False,
'element7':0,
'element8':9
}
Suppose that I want to print the reciprocal of each value inside the dictionary. So this is what I did:
print(f'Reciprocal of {v} is {1/v}')
It is going to generate errors since we can divide 0 or string or Boolean data types. Write the code in a way that if reciprocal is possible, it will print that as:
Reciprocal of 5 is 0.2.
And if reciprocal is not possible, it will print that it's not possible along with the description of the exception. For example in case of 0, this should be displayed:
Reciprocal of 0 is not possible. Exception : (division by zero)
2- I am applying a file read operation using the try/except/else/finally clause:
try:
except FileNotFoundError:
print('File does not exist!')
else:
finally:
if 'f' in locals():
This works fine but suppose in the else clause, instead of reading the file, I have written a statement to write content as:
This is going to generate error since file is opened in the read mode. We can open the file in read+write mode, but for some reasons I want the file be opened in read mode always and writing should not be allowed. Mange the code with nested try/clause such that if write operation is attempted, an appropriate message is printed instead of an exception.
#PythonProgramming #python #pythontutorial
In this lesson we will study about Exception Handling. Exceptions are the errors which will terminate the program execution and as developer we must consider every possible way to handle these errors.
In python we have try and except statements to handle any exception in the program. With try and except, there can be else and finally clauses. We will see the use of these four clauses with different examples.
Then there is also a way to generate an exception, known as raise the exception. We usually need this in Object Oriented Programming. We will use that there but will just see how we can raise an exception.
Moreover, we will see Python and Non-Python way of handling the exception. One is known as Look Before You Leap and second is It's Easy to Ask Forgiveness than Permission.
Complete Playlist:
If you have the basic programming knowledge and interested to learn Object-Oriented Programming in Python, check out this playlist:
Lab Manual 26 can be downloaded from here:
Review Question:
1- Suppose I have a Dictionary with 8 key-value pairs as shown here:
d={
'element1':5,
'element2':2,
'element3':0,
'element4':'Computer',
'element5':3.2,
'element6':False,
'element7':0,
'element8':9
}
Suppose that I want to print the reciprocal of each value inside the dictionary. So this is what I did:
print(f'Reciprocal of {v} is {1/v}')
It is going to generate errors since we can divide 0 or string or Boolean data types. Write the code in a way that if reciprocal is possible, it will print that as:
Reciprocal of 5 is 0.2.
And if reciprocal is not possible, it will print that it's not possible along with the description of the exception. For example in case of 0, this should be displayed:
Reciprocal of 0 is not possible. Exception : (division by zero)
2- I am applying a file read operation using the try/except/else/finally clause:
try:
except FileNotFoundError:
print('File does not exist!')
else:
finally:
if 'f' in locals():
This works fine but suppose in the else clause, instead of reading the file, I have written a statement to write content as:
This is going to generate error since file is opened in the read mode. We can open the file in read+write mode, but for some reasons I want the file be opened in read mode always and writing should not be allowed. Mange the code with nested try/clause such that if write operation is attempted, an appropriate message is printed instead of an exception.
#PythonProgramming #python #pythontutorial
Комментарии