10-27-2021 05:10 AM
Let's say I want to check if a condition is false then stop the execution of the rest of the script.
I tried with two approaches:
1) raising exception
if not data_input_cols.issubset(data.columns):
raise Exception("Missing column or column's name missmatch. Please check input data has a valid schema: " + str(data_input_cols))
Even if the exception is thrown it still continue running the next cell
2) exit notebook
if not data_input_cols.issubset(data.columns):
dbutils.notebook.exit("Missing column or column's name missmatch. Please check input data has a valid schema: " + str(data_input_cols))
Here only the message is thrown. Again the rest of the cells are executed
TIA
10-27-2021 05:16 AM
the notebook behavior depends on how you execute the cells.
If you do 'run all below' , the notebook keeps on going even with an exception.
If you do 'run all' which is basically the same as scheduling it in a job, this is not the case.
Check this topic for more detail:
As you can see: a feature request has been made to be able to change this behavior.
10-27-2021 05:16 AM
the notebook behavior depends on how you execute the cells.
If you do 'run all below' , the notebook keeps on going even with an exception.
If you do 'run all' which is basically the same as scheduling it in a job, this is not the case.
Check this topic for more detail:
As you can see: a feature request has been made to be able to change this behavior.
10-27-2021 06:57 AM
You are right. With "run all" it works. Thanks for explanation!
10-31-2023 05:33 AM
In Jupyter notebooks or similar environments, you can stop the execution of a notebook at a specific cell by raising an exception. However, you need to handle the exception properly to ensure the execution stops. The issue you're encountering could be due to unhandled exceptions.
Here's how you can stop the execution at a specific cell using an exception:
```python
if not data_input_cols.issubset(data.columns):
raise Exception("Missing column or column's name missmatch. Please check input data has a valid schema: " + str(data_input_cols))
```
To make sure the execution stops when the exception is raised, you can add a try-except block in the cells where you want to capture the exception. For example:
```python
try:
# Your code here
except Exception as e:
# Handle the exception if needed
print(f"An exception occurred: {str(e)}")
```
This way, when the exception is raised, the code execution in that cell will stop, and you can choose to handle the exception as required. The subsequent cells will not be executed.
If you're using a different environment or have specific requirements, please provide more details for a more tailored solution.
Passionate about hosting events and connecting people? Help us grow a vibrant local community—sign up today to get started!
Sign Up Now