<?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: How to catch exception from dbutils.widgets.get(...) in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/how-to-catch-exception-from-dbutils-widgets-get/m-p/16711#M10841</link>
    <description>&lt;P&gt;I handle this exception with something like:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;import py4j
 
try:
  value = dbutils.widgets.get("parameter")
except py4j.protocol.Py4JJavaError as e:
  print(e)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;If you look more closely at the stack trace you'll see the origin of the message is from some Java code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;/databricks/spark/python/lib/py4j-0.10.9.1-src.zip/py4j/protocol.py in get_return_value(answer, gateway_client, target_id, name)
    325             if answer[1] == REFERENCE_TYPE:
--&amp;gt; 326                 raise Py4JJavaError(
    327                     "An error occurred while calling {0}{1}{2}.\n".
&amp;nbsp;
Py4JJavaError: An error occurred while calling o318.getArgument.
: com.databricks.dbutils_v1.InputWidgetNotDefined: No input widget named parameter is defined&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;"com.databricks.dbutils_v1.InputWidgetNotDefined" appears to be a Java class and the actual Python exception to handle is "Py4JJavaError".&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 24 May 2022 04:49:16 GMT</pubDate>
    <dc:creator>blt</dc:creator>
    <dc:date>2022-05-24T04:49:16Z</dc:date>
    <item>
      <title>How to catch exception from dbutils.widgets.get(...)</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-catch-exception-from-dbutils-widgets-get/m-p/16710#M10840</link>
      <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;I'm trying to write python notebook code that can be run from databricks web ui or from airflow. I intend to pass parameters from airflow via the job api using &lt;PRE&gt;&lt;CODE&gt;notebook_params&lt;/CODE&gt;&lt;/PRE&gt;. From what I understand, these are accessible as widget values. &lt;/P&gt;
&lt;P&gt;&lt;PRE&gt;&lt;CODE&gt;dbutils.widgets.get&lt;/CODE&gt;&lt;/PRE&gt; does not appear to accept a default value option so I assume I'll just have to catch an exception in case there isn't a value available. The code thrown on the UI ( &lt;PRE&gt;&lt;CODE&gt;InputWidgetNotDefined&lt;/CODE&gt;&lt;/PRE&gt; ) doesn't appear to work when I try catching it: &lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;try:
  test_val = dbutils.widgets.get("test_name")
except InputWidgetNotDefined:
  test_val = "some default value"
print(test_val)
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I can do a general, empty &lt;PRE&gt;&lt;CODE&gt;except:&lt;/CODE&gt;&lt;/PRE&gt; which will work but is generally bad practice to do blanket catches if we can avoid it.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;try:
  test_val = dbutils.widgets.get("test_name")
except:
  test_val = "some default value"
print(test_val)
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So is there a more precise exception I can catch? &lt;/P&gt; 
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Aug 2021 16:16:59 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-catch-exception-from-dbutils-widgets-get/m-p/16710#M10840</guid>
      <dc:creator>entimaniac</dc:creator>
      <dc:date>2021-08-09T16:16:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to catch exception from dbutils.widgets.get(...)</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-catch-exception-from-dbutils-widgets-get/m-p/16711#M10841</link>
      <description>&lt;P&gt;I handle this exception with something like:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;import py4j
 
try:
  value = dbutils.widgets.get("parameter")
except py4j.protocol.Py4JJavaError as e:
  print(e)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;If you look more closely at the stack trace you'll see the origin of the message is from some Java code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;/databricks/spark/python/lib/py4j-0.10.9.1-src.zip/py4j/protocol.py in get_return_value(answer, gateway_client, target_id, name)
    325             if answer[1] == REFERENCE_TYPE:
--&amp;gt; 326                 raise Py4JJavaError(
    327                     "An error occurred while calling {0}{1}{2}.\n".
&amp;nbsp;
Py4JJavaError: An error occurred while calling o318.getArgument.
: com.databricks.dbutils_v1.InputWidgetNotDefined: No input widget named parameter is defined&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;"com.databricks.dbutils_v1.InputWidgetNotDefined" appears to be a Java class and the actual Python exception to handle is "Py4JJavaError".&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 24 May 2022 04:49:16 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-catch-exception-from-dbutils-widgets-get/m-p/16711#M10841</guid>
      <dc:creator>blt</dc:creator>
      <dc:date>2022-05-24T04:49:16Z</dc:date>
    </item>
  </channel>
</rss>

