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

dbutils.notebook.exit() executing from except in try/except block even if there is no error.

Roy
New Contributor II

I am using Python notebooks as part of a concurrently running workflow with Databricks Runtime 6.1.

Within the notebooks I am using try/except blocks to return an error message to the main concurrent notebook if a section of code fails. However I have noticed that in the last block of execution the "except" exit text will be returned regardless of an error in the "try" block.

Running this code highlights the problem:

try:

a = 5

dbutils.notebook.exit("a equals 5")

except:

dbutils.notebook.exit("Still printing exception")

1 ACCEPTED SOLUTION

Accepted Solutions

shyam_9
Valued Contributor
Valued Contributor

Hey @Roy,

It's working for me,

0693f000007Oro0AAC

Check the indentation correctly.

View solution in original post

5 REPLIES 5

shyam_9
Valued Contributor
Valued Contributor

Hey @Roy,

It's working for me,

0693f000007Oro0AAC

Check the indentation correctly.

Roy
New Contributor II

That's odd, it's working for me now also. I tried this in different notebooks attached to the same cluster, I wonder if it was some odd cluster behavior. Thanks for checking.

akshaylad
New Contributor II

dbutils.notebook.exit() will raise an exception

In the answer provided by @Shyamprasad Miryala​  above the print inside of except does not get printed because notebook.exit() text takes priority over any other print().

Look at this example:

%python a = 0 try: a = 1 dbutils.notebook.exit("Inside try") except Exception as ex: a = 2 dbutils.notebook.exit("Inside exception")

Output: Notebook exited: Inside exception

%python print(a)

Output: 2

sivasankar
New Contributor II

SO if i want to exit with try block dbutils.notebook.exit("Inside try").What is the process?

vivekvardhanSha
New Contributor II

You can add a fake except for the notebook.exit inside try block

try:

notebook.run(somenotebook)

try:

notebook.exit()

except Exception as e

print("Notebook exited")

except:

print("Main exception")

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.