
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Suggest a cleaner way to handle Python exceptions?
We can use the finally clause to clean up whether an exception is thrown or not:
try: #some code here except: handle_exception() finally: do_cleanup()
If the cleanup is to be done in the event of an exception, we can code like this:
should_cleanup = True try: #some code here should_cleanup = False except: handle_exception() finally: if should_cleanup(): do_cleanup()
- Related Articles
- How to Handle Exceptions in Ruby
- How to handle Exceptions while working with JDBC applications?
- What is the best way to handle list empty exception in Python?
- Suggest some good usernames for my twitter handle.
- Can anyone suggest how to pass time the best possible way?
- What is the best way to handle a Javascript popup using Selenium Webdriver?
- How can we create an exception filter to handle unhandled exceptions in C#\nASP.NET WebAPI?
- Concrete Exceptions in Python
- How to handle a python exception within a loop?
- Raising an Exceptions in Python
- User-Defined Exceptions in Python
- Are Python Exceptions runtime errors?
- How to declare custom exceptions in modern Python?
- How to handle exception inside a Python for loop?
- How expensive are Python dictionaries to handle?

Advertisements