Python Pickling and Exception Handling (2)
Python Pickling and Exception Handling (2)
Python Pickling
Python Pickling
• Python pickle module is used for serializing and de-serializing
python object structures.
• Precaution:
• advisable not to unpickle data received from an untrusted source as they may
pose security threat
import pickle
mylist = ['a', 'b', 'c', 'd']
with open('datafile.txt', 'wb') as fh:
pickle.dump(mylist, fh)
Unpickle
• Unpickle a simple list: unpickle_list1.py
import pickle
pickle_off = open ("datafile.txt", "rb")
emp = pickle.load(pickle_off)
print(emp)
Unpickle
Pickling
Python - Exceptions
Python - Exceptions
• An exception is an event, which occurs during the execution of a
program that disrupts the normal flow of the program's instructions.
• ArithmeticError
• Base class for all errors that occur for numeric calculation
• ZeroDivisionError
• Raised when division or modulo by zero takes place for all numeric types
• AttributeError
• Raised in case of failure of attribute reference or assignment.
• X = 10
• # Raises an AttributeError
• X.append(6)
Standard Exception examples
• KeyboardInterrupt
• Raised when the user interrupts program execution, usually by pressing Ctrl+c.
• IndexError
• Raised when an index is not found in a sequence.
• KeyError
• Raised when the specified key is not found in the dictionary.
• NameError
• Raised when an identifier is not found in the local or global namespace.
• geek = input()
• print(geeky)
Standard Exception examples
• IOError
• Raised when an input/ output operation fails, such as open() function when
trying to open a file that does not exist.
• SyntaxError
• Raised when there is an error in Python syntax.
• IndentationError
• Raised when indentation is not specified properly.
• RuntimeError
• Raised when a generated error does not fall into any category.
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.
• try-finally clause
Handling an exception
The except Clause with Multiple Exceptions
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 try-finally Clause
The try-finally Clause
• You cannot use else clause as well along with a finally clause.
The try-finally clause
try:
try:
with open("demo.txt", “r") as file:
data = file.readlines()
for line in data:
word = line.split()
print (word)
finally:
print("this should be executed")
except IOError:
print("Error") You cannot use else clause as well along with a finally clause.
else:
print("No Error")
The try-finally clause
• 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
Argument of an Exception