cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
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
Databricks Employee
Databricks Employee

Hey @Roy,

It's working for me,

0693f000007Oro0AAC

Check the indentation correctly.

View solution in original post

6 REPLIES 6

shyam_9
Databricks Employee
Databricks Employee

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")

tonyliken
New Contributor

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:

tonyliken_0-1737044717238.png

error in query:

tonyliken_1-1737044738267.png

 

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group