Search code examples
apache-sparkpysparkdatabricksazure-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?


Solution

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