- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do you properly ignore Exceptions in Python?
This can be done by following codes
try: x,y =7,0 z = x/y except: pass
try: x,y =7,0 z = x/y except Exception: pass
These codes bypass the exception in the try statement and ignore the except clause and don’t raise any exception.
The difference in the above codes is that the first one will also catch KeyboardInterrupt, SystemExit etc, which are derived directly from exceptions.BaseException, not exceptions.Exception.
It is known that the last thrown exception is remembered in Python, some of the objects involved in the exception-throwing statement are kept live until the next exception. We might want to do the following instead of just pass:
try: x,y =7,0 z = x/y except Exception: sys.exc_clear()
This clears the last thrown exception
- How do I truncate tables properly in MySQL?
- How do exceptions work in C++
- How do you create nested dict in Python?
- How do you overlap widgets/frames in Python tkinter?
- Concrete Exceptions in Python
- How do you earn bitcoins?
- How do you append to a file with Python?
- How do you compare Python objects with .NET objects?
- How do you round up a float number in Python?
- Raising an Exceptions in Python
- User-Defined Exceptions in Python
- How do you know when someone really loves you?
- How to ignore hidden files using os.listdir() in Python?
- How to ignore an exception and proceed in Python?
- How to setup VIM autoindentation properly for editing Python files?