<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: dbutils.notebook.exit() executing from except in try/except block even if there is no error. in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/dbutils-notebook-exit-executing-from-except-in-try-except-block/m-p/27558#M19424</link>
    <description>&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hey @Roy,&lt;/P&gt;&lt;P&gt;It's working for me,&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="0693f000007Oro0AAC"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/2512iC5916C279E8F2434/image-size/large?v=v2&amp;amp;px=999" role="button" title="0693f000007Oro0AAC" alt="0693f000007Oro0AAC" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Check the indentation correctly. &lt;/P&gt;</description>
    <pubDate>Tue, 19 Nov 2019 07:25:19 GMT</pubDate>
    <dc:creator>shyam_9</dc:creator>
    <dc:date>2019-11-19T07:25:19Z</dc:date>
    <item>
      <title>dbutils.notebook.exit() executing from except in try/except block even if there is no error.</title>
      <link>https://community.databricks.com/t5/data-engineering/dbutils-notebook-exit-executing-from-except-in-try-except-block/m-p/27557#M19423</link>
      <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;I am using Python notebooks as part of a concurrently running workflow with Databricks Runtime 6.1. &lt;/P&gt;
&lt;P&gt;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. &lt;/P&gt;
&lt;P&gt;Running this code highlights the problem:&lt;/P&gt;
&lt;P&gt;try: &lt;/P&gt;
&lt;P&gt; a = 5 &lt;/P&gt;
&lt;P&gt; dbutils.notebook.exit("a equals 5") &lt;/P&gt;
&lt;P&gt;except: &lt;/P&gt;
&lt;P&gt; dbutils.notebook.exit("Still printing exception") &lt;/P&gt; 
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Nov 2019 20:59:25 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/dbutils-notebook-exit-executing-from-except-in-try-except-block/m-p/27557#M19423</guid>
      <dc:creator>Roy</dc:creator>
      <dc:date>2019-11-18T20:59:25Z</dc:date>
    </item>
    <item>
      <title>Re: dbutils.notebook.exit() executing from except in try/except block even if there is no error.</title>
      <link>https://community.databricks.com/t5/data-engineering/dbutils-notebook-exit-executing-from-except-in-try-except-block/m-p/27558#M19424</link>
      <description>&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hey @Roy,&lt;/P&gt;&lt;P&gt;It's working for me,&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="0693f000007Oro0AAC"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/2512iC5916C279E8F2434/image-size/large?v=v2&amp;amp;px=999" role="button" title="0693f000007Oro0AAC" alt="0693f000007Oro0AAC" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Check the indentation correctly. &lt;/P&gt;</description>
      <pubDate>Tue, 19 Nov 2019 07:25:19 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/dbutils-notebook-exit-executing-from-except-in-try-except-block/m-p/27558#M19424</guid>
      <dc:creator>shyam_9</dc:creator>
      <dc:date>2019-11-19T07:25:19Z</dc:date>
    </item>
    <item>
      <title>Re: dbutils.notebook.exit() executing from except in try/except block even if there is no error.</title>
      <link>https://community.databricks.com/t5/data-engineering/dbutils-notebook-exit-executing-from-except-in-try-except-block/m-p/27559#M19425</link>
      <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;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. &lt;/P&gt; 
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Nov 2019 13:21:51 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/dbutils-notebook-exit-executing-from-except-in-try-except-block/m-p/27559#M19425</guid>
      <dc:creator>Roy</dc:creator>
      <dc:date>2019-11-19T13:21:51Z</dc:date>
    </item>
    <item>
      <title>Re: dbutils.notebook.exit() executing from except in try/except block even if there is no error.</title>
      <link>https://community.databricks.com/t5/data-engineering/dbutils-notebook-exit-executing-from-except-in-try-except-block/m-p/27560#M19426</link>
      <description>&lt;P&gt;dbutils.notebook.exit() will raise an exception&lt;/P&gt;&lt;P&gt;In the answer provided by @Shyamprasad Miryala​&amp;nbsp; above the print inside of except does not get printed because notebook.exit() text takes priority over any other print().&lt;/P&gt;&lt;P&gt;Look at this example:&lt;/P&gt;&lt;P&gt;%python a = 0 try: a = 1 dbutils.notebook.exit("Inside try") except Exception as ex: a = 2 dbutils.notebook.exit("Inside exception")&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Output: Notebook exited: Inside exception&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%python print(a)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Output: 2 &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 06:09:26 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/dbutils-notebook-exit-executing-from-except-in-try-except-block/m-p/27560#M19426</guid>
      <dc:creator>akshaylad</dc:creator>
      <dc:date>2020-06-25T06:09:26Z</dc:date>
    </item>
    <item>
      <title>Re: dbutils.notebook.exit() executing from except in try/except block even if there is no error.</title>
      <link>https://community.databricks.com/t5/data-engineering/dbutils-notebook-exit-executing-from-except-in-try-except-block/m-p/27561#M19427</link>
      <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;SO if i want to exit with try block dbutils.notebook.exit("Inside try").What is the process?&lt;/P&gt; 
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Oct 2020 11:20:38 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/dbutils-notebook-exit-executing-from-except-in-try-except-block/m-p/27561#M19427</guid>
      <dc:creator>sivasankar</dc:creator>
      <dc:date>2020-10-07T11:20:38Z</dc:date>
    </item>
    <item>
      <title>Re: dbutils.notebook.exit() executing from except in try/except block even if there is no error.</title>
      <link>https://community.databricks.com/t5/data-engineering/dbutils-notebook-exit-executing-from-except-in-try-except-block/m-p/27562#M19428</link>
      <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;You can add a fake except for the notebook.exit inside try block&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;try:
&lt;P&gt;notebook.run(somenotebook)&lt;/P&gt;
&lt;P&gt;try:&lt;/P&gt;
&lt;P&gt;notebook.exit()&lt;/P&gt;
&lt;P&gt;except Exception as e&lt;/P&gt;
&lt;P&gt;print("Notebook exited")&lt;/P&gt;
&lt;P&gt;except:&lt;/P&gt;
&lt;P&gt;print("Main exception")&lt;/P&gt; 
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Aug 2021 09:07:59 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/dbutils-notebook-exit-executing-from-except-in-try-except-block/m-p/27562#M19428</guid>
      <dc:creator>vivekvardhanSha</dc:creator>
      <dc:date>2021-08-04T09:07:59Z</dc:date>
    </item>
    <item>
      <title>Re: dbutils.notebook.exit() executing from except in try/except block even if there is no error.</title>
      <link>https://community.databricks.com/t5/data-engineering/dbutils-notebook-exit-executing-from-except-in-try-except-block/m-p/105940#M42323</link>
      <description>&lt;P&gt;because the dbutils.notebook.exit() is an 'Exception' it will always trigger the except Exception as e: part of the code.&amp;nbsp;&lt;/P&gt;&lt;P&gt;When can use this to our advantage to solve the problem by adding an 'if else' to the except block.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;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. &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;correct query:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="tonyliken_0-1737044717238.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/14182i5C16899F070D5217/image-size/medium?v=v2&amp;amp;px=400" role="button" title="tonyliken_0-1737044717238.png" alt="tonyliken_0-1737044717238.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;error in query:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="tonyliken_1-1737044738267.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/14183i77DA2DF7C4DA3E56/image-size/medium?v=v2&amp;amp;px=400" role="button" title="tonyliken_1-1737044738267.png" alt="tonyliken_1-1737044738267.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2025 16:26:32 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/dbutils-notebook-exit-executing-from-except-in-try-except-block/m-p/105940#M42323</guid>
      <dc:creator>tonyliken</dc:creator>
      <dc:date>2025-01-16T16:26:32Z</dc:date>
    </item>
  </channel>
</rss>

