how to get and set the error details to a variable when a command fails in a notebook

80 Views Asked by At

Am new to databricks . I have a notebook that has multiple commands . I want to capture the specific error message that is thrown when any of the commands fail. basically I would want to customize the error message in order to be able to identify which command of the notebook failed or caused the failure . In my Notebook each command performs a specific task and I want to know which task failed . How or what should I include to capture the error details ?

Currently we are just setting a generic error message when the notebook activity failed , but we are trying to find a mechanism that would give us more info on which command in the notebook failed that caused the notebook activity to fail

1

There are 1 best solutions below

0
Rakesh Govindula On

You can use traceback module to store the error as suggested by @John Gordon in comments.

Use below code and keep your code in try block.

import traceback

#Variable for the error
a=''
try:
    ...Your code...
except:
    # Store the error in variable
    a=traceback.format_exc()
print(a)

Example:

enter image description here

This will give the exact information about the error and error line in your code.

You can also store your error in a file with traceback.print_exc(file=fileObject).