Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2025 08:26 AM
because the dbutils.notebook.exit() is an 'Exception' it will always trigger the except Exception as e: part of the code.
When can use this to our advantage to solve the problem by adding an 'if else' to the except block.
query = "SELECT 'a' as Column"
try:
spark.sql(query) ##if this part fails it will trigger and exception with the actual error message from the failure when running the query.
dbutils.notebook.exit(f'Success: SQL query completed') ##notebook.exit() is classed as an exception and therefore triggers the exception part below, we add custom text so we can check for it in the exception part of the code
except Exception as e:
if str(e) == 'Success: SQL query completed': ##we check here for the custom text we added above, meaning the notebook code actually completed successfully. (we have to conver the exception (e) to string to do that
dbutils.notebook.exit(f"Success: SQL query completed") ## exit the notebook with a custom success message. as there is no more 'except Exception....' checks in the code this will exit the notebook
else:
dbutils.notebook.exit(f"Error: {str(e)}") ## exit the notebook with a Error: followed by the actual error messaage from the notebook code (agian we converted it to a string. correct query:
error in query: