cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
cancel
Showing results for 
Search instead for 
Did you mean: 

How to kill the execution of a notebook on specyfic cell?

Braxx
Contributor II

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

1 ACCEPTED SOLUTION

Accepted Solutions

-werners-
Esteemed Contributor III

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:

https://community.databricks.com/s/question/0D53f00001PonToCAJ/executing-notebooks-run-all-cells-vs-...

As you can see: a feature request has been made to be able to change this behavior.

View solution in original post

3 REPLIES 3

-werners-
Esteemed Contributor III

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:

https://community.databricks.com/s/question/0D53f00001PonToCAJ/executing-notebooks-run-all-cells-vs-...

As you can see: a feature request has been made to be able to change this behavior.

You are right. With "run all" it works. Thanks for explanation!

Invasioned
New Contributor II

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.

All about tech and apps
Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.