Problem – Reraising the exception, that has been caught in the except block.
def example(): try: int('N/A') except ValueError: print("Didn't work") raise example() |
Output :
Didn't work
Traceback (most recent call last):
File "", line 1, in
File "", line 3, in example
ValueError: invalid literal for int() with base 10: 'N/A'
This problem typically arises when there is no need to take any action in response to an exception (e.g., logging, cleanup, etc.). A very common use might be in catch-all exception handlers.
Code #2 : Catching all exception handlers.
try: ... except Exception as e: # Process exception information in some way ... # Propagate the exception raise |
Problem 2 – To have a program issue warning messages (e.g., about deprecated features or usage problems).
Code #3: Using the warnings.warn() function
import warnings def func(x, y, logfile = None, debug = False): if logfile is not None: warnings.warn('logfile argument deprecated', DeprecationWarning) |
The arguments to warn() are a warning message along with a warning class, which is typically one of the following:
UserWarning, DeprecationWarning, SyntaxWarning, RuntimeWarning, ResourceWarning, or FutureWarning.
The handling of warnings depends on how the interpreter is executed and other configuration.
Output when running Python with the -W all option.
bash % python3 -W all example.py
example.py:5: DeprecationWarning: logfile argument is deprecated
warnings.warn('logfile argument is deprecated', DeprecationWarning)
Normally, warnings just produce output messages on standard error. To turn warnings into exceptions, use the -W error option.
bash % python3 -W error example.py
Traceback (most recent call last):
File "example.py", line 10, in
func(2, 3, logfile ='log.txt')
File "example.py", line 5, in func
warnings.warn('logfile argument is deprecated', DeprecationWarning)
DeprecationWarning: logfile argument is deprecated
bash %
Recommended Posts:
- Python | Issue Warning Message
- Python | Raising an Exception to Another Exception
- Python Exception Handling
- Multiple Exception Handling in Python
- Create an Exception Logging Decorator in Python
- How to pass argument to an Exception in Python?
- Test if a function throws an exception in Python
- How to print exception stack trace in Python?
- Handling TypeError Exception in Python
- How to log a Python exception?
- How to print the Python Exception/Error Hierarchy?
- EnvironmentError Exception in Python
- Handling NameError Exception in Python
- Handling OSError exception in Python
- How to handle a Python Exception in a List Comprehension?
- Handling a thread's exception in the caller thread in Python
- Handling EOFError Exception in Python
- Python program to interchange first and last elements in a list
- Python | Get first and last elements of a list
- Python | Merge first and last elements separately in a list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

