Defining Clean Up Actions:

Suppose we want to perform a task from a program. Let us look at an example- Suppose we use try statement which has an optical clause, “finally” for performing clean up actions. This must be executed under all circumstances.

Cleanup actions: If we want to leave the try statement, “finally” clause is always executed. It does not matter whether exception is raised or not. These are the clauses that are intended to define clean up actions that have to be executed under all circumstances.

When an exception occurs, and it is not handled by except clause, “finally” will occur first and the error is raised as default.

Code 1: The code will work normally, and clean-up action takes place at the end.

Code 2: This code will raise an error, and it will be carefully handled in the except clause. Clean up action takes place at the end.

Code 3: This code raises an error, but we don’t have except clause to handle it. First, clean-up action will be taken, and then an error is raised by the compiler by default.

Error:

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.