
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
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 Articles
- 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?
- How do you handle an exception thrown by an except clause in Python?
- What is the difference between 'except Exception as e' and 'except Exception, e' in Python?
- Is it possible to catch multiple Java exceptions in single catch block?
- Can we declare a try catch block within another try catch block in Java?
- How to raise an exception in Python?
- Is it necessary that a try block should be followed by a catch block in Java?
- Catch block and type conversion in C++
- Is it possible to have multiple try blocks with only one catch block in java?
- Can we have a try block without a catch block in Java?\n
- Can we have an empty catch block in Java?
- Can we to override a catch block in java?
- try and except in Python
- What is the difference between throw e and throw new Exception(e) in catch block in java?

Advertisements