- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2025 05:06 AM
In a Python notebook, I am using error handling according to the official documentation.
try:
[some data transformation steps]
except PySparkException as ex:
[logging steps to log the error condition and error message in a table]
However, this catches only PySpark exception classes, and not all exceptions in the code. To catch the remaining exceptions, I would need to add the Python exception handler:
try:
[some data transformation steps]
except Exception as ex:
[logging steps to log the error condition and error message in a table]
My aim is to be able to handle both exceptions. If I use only the except PySparkException, it does not catch Python exceptions, if I use only except Exception, I cannot use the error condition and message for logging. I tried to do both, by embedding the try - PySparkException in a try - Exception. This works well for Python exceptions, they are not caught by PySparkException, but are then caught by Exception. However, in case of PySparkException, they are caught twice which makes logging complicated (error code and error message is overwritten by the second exception handler).
Is there a way to define the outer, Python Exception in a way that it only catches exceptions that are not caught by PySparkException?