Change language

Defining Cleanup Actions in Python

|

Think of the task that you always want your program to do, whether it works fine or throws any errors. For example, we use the try operator with the optional —  "finally" to perform cleanup actions that must be performed under all conditions. 
Cleanup Actions: before exiting the try statement, a finally clause is always executed, regardless of whether any exception has been thrown or not. These are points for defining the cleaning actions to be carried out under all circumstances. 
Whenever an exception is thrown and it is not handled by the exception clause, finally occurs first, and then the default error [Code 3] occurs.

Python programs illustrating "Determine Cleanup Actions"

Code 1: Code runs fine and action is taken at the end by clearing

# Python code for illustration
# clear actions

def divide (x, y):

try :

  # Gender department: gives only fractional part as an answer

  result = x / / y

except ZeroDivisionError:

print ( "Sorry! You are dividing by zero " )

  else :

print ( "Yeah! Your answer is:" , result)

finally :

print ( "I’m finally clause, always raised !!" )

 
# Look at the parameters and notice how the program works

divide ( 3 , 2 )

Output:

 Yeah! Your answer is: 1 I’m finally clause, always raised !! 

Code 2: The error is in the code and is carefully handled in a exception . Note that the cleanup action is performed at the end.

# Python code for illustration
# clear actions

def divide (x, y):

try :

# Gender department: gives only fractional part as answer

  result = x / / y

  except ZeroDivisionError:

  print ( " Sorry! You are dividing by zero " )

  else :

print ( "Yeah! Your answer is:" , result)

finally :

print ( "I’m finally clause, always raised !!" )

 
# Look at the parameters and notice how the program works

divide ( 3 , 0 )

Output:

 Sorry! You are dividing by zero I’m finally clause, always raised !! 

Code 3: Code, raise a bug, but we have nothing except a caveat to handle it. So the cleanup is done first, and then the compiler throws an error (default) .

# Python code for illustration
# clear actions

def divide (x , y):

try :

# Gender department: gives only fractional part as an answer

result = x / / y

except ZeroDivisionError: 

print ( "Sorry! You are dividing by zero " )

  else :

print ( "Yeah! Your answer is:" , result)

finally :

print ( "I’m finally clause, always raised !!" )

 
# Look at the parameters and notice how the program works

divide ( 3 , "3" )

Exit:

 I’m finally clause, always raised !! 

Error :

 Traceback (most recent call last): File "C: /Users/DELL/Desktop/Code.py", line 15, in divide (3, "3") File "C: /Users/DELL/Desktop/Code.py", line 7, in divide result = x // y TypeError: unsupported operand type (s) for //: ’int ’and’ str’ 

This article is courtesy of Mohit Gupta_OMG

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically