0

mssparkutils.notebook.exit isn't exiting properly when used in try block, and it is rising exception. Can someone help me understand, why it isn't working inside try block? and how to make it work?

enter image description here enter image description here

it worked, if not using try block.

enter image description here enter image description here

subro
  • 1,167
  • 4
  • 20
  • 32

1 Answers1

1

when I am executing mssparkutils.notebook.exit(Id) with try except block of code:

import logging
import traceback
try:
  if True:  
    df = spark.createDataFrame(
        [
                (1, "AA"), 
                (2, "BB"),
        ],
        ["id", "Name"] 
    )
    Id = df.select('id').rdd.map(lambda x: x[0]).first()

    mssparkutils.notebook.exit(Id)

except:
   logging.error(traceback.format_exc())
   

I got the same output:

enter image description here

In above code snippet If an exception is raised within the try block, the except block executes, and the traceback of the exception is logged using the logging.error() function. However, after catching the exception, the code continues executing the rest of the program, and the traceback is displayed as the output.

As per this MS Document When you call an exit() function from a notebook interactively, Azure Synapse will throw an exception, that may be the reason it is going to exception block and giving the output as above.

Due to exception, it is giving null as exit value while executing the notebook activity. If you want to get exit value with, try-except block then call exit value in except block as mentioned below:

import logging
import traceback
try:
  if True:  
    df = spark.createDataFrame(
        [
                (1, "AA"), 
                (2, "BB"),
        ],
        ["id", "Name"] 
    )
    Id = df.select('id').rdd.map(lambda x: x[0]).first()

    mssparkutils.notebook.exit(Id)

except:
   logging.error(traceback.format_exc())
   mssparkutils.notebook.exit(Id)

You will get the exit value as mentioned below:

enter image description here

enter image description here

Bhavani
  • 1,725
  • 1
  • 3
  • 6
  • Thanks for the comment, but the notebook will be treated as failed, isn't it? @Bhavani – subro Jul 24 '23 at 12:43
  • Yes, the notebook is considered "failed" because an exception was raised, but it is not a typical failure. It's better to execute notebook without try-except block. – Bhavani Jul 24 '23 at 12:51
  • Which I understand, to make this work, it has to be without try except, But the point, is, it shouldn't through exception. – subro Jul 24 '23 at 13:02