azure-synapseazure-notebooks

mssparkutils.notebook.exit in Try block


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


Solution

  • 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