
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 to raise an exception in one except block and catch it in a later except block in Python?
Only a single except clause in a try block is invoked. If you want the exception to be caught higher up then you will need to use nested try blocks.
Let us write 2 try...except blocks like this:
try: try: 1/0 except ArithmeticError as e: if str(e) == "Zero division": print ("thumbs up") else: raise except Exception as err: print ("thumbs down") raise err
we get the following output
thumbs down Traceback (most recent call last): File "C:/Users/TutorialsPoint1/~.py", line 11, in <module> raise err File "C:/Users/TutorialsPoint1/~.py", line 3, in <module> 1/0 ZeroDivisionError: division by zero
As per python tutorial there is one and only one caught or catched exception per one try statement.
- Related Questions & Answers
- How to catch multiple exceptions in one line (except block) in Python?
- How can I write a try/except block that catches all Python exceptions?
- Is it possible to catch multiple Java exceptions in single catch block?
- Is it necessary that a try block should be followed by a catch block in Java?
- Can we declare a try catch block within another try catch block in Java?
- Catch block and type conversion in C++
- Can we have a try block without a catch block in Java?
- What is the difference between 'except Exception as e' and 'except Exception, e' in Python?
- How to throw an exception from a static block in Java?
- How to raise an exception in Python?
- Is it possible to have multiple try blocks with only one catch block in java?
- How do you handle an exception thrown by an except clause in Python?
- Can we have an empty catch block in Java?
- try and except in Python
- Can we to override a catch block in java?
Advertisements