How to Throw Exception in Databricks?

How to Throw Exception in Databricks?

I want my Databricks notebook to fail if a certain condition is satisfied. Right now I am using dbutils.notebook.exit() but it does not cause the notebook to fail and I will get mail like notebook run is successful. How can I make my notebook fail?

1

3 Answers

Correct, although dbutils.notebook.exit("Custom message") makes the job skip rest of the commands, the job is marked as succeeded. We can use raise Exception if its a python notebook. This will also skip the rest of the commands, but mark the job as failed.

if condition: 
  raise Exception("Custom message")

For me the better way was to re-raise the same exception I got after handling... I've added some reporting I need in except: step, but then reraise, so job has status FAIL and logged exception in the last cell result.

Example:

try:
    run_my_job()
except Exception as err:
    log_to_some_place(f"{JOB_NAME} has failed, RunID: {mu_run_id}")
    raise

I am on the same boat and for sure dbutils.notebook.exit() is not helping in failing a job .There can be better way to get this done , but the below piece of code will make the job fail .

status = 'Fail'
if(status=='Fail'):
  10/0
else:
  100/10
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Maya Lin-Takahashi
Author

Maya Lin-Takahashi

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.