How to get it out a variable in python code using UiPath?

Hi All,

I have run a Python code and its get exception while running a script, and here need to save a exception into a variable and need to get a exception variable using UiPath.

Can any one please help to this

Thanks in Advance…

Hi @keerthi_arumugam ,

Try following way’s

  1. Use the Try-Catch activity to capture the error message into the catch block and assign the same to variable.

  2. In your Python script use exception handling ( Try and Except method) to catch the exception and save the exception message into the variable in the Python script and use Activities - Invoke Python Method (uipath.com) to get the The result of the invoked method,
    Activities - Invoke Python Method (uipath.com)

1 Like

Hello @keerthi_arumugam

Assign activity:

  • Variable: pythonScript
  • Value: “try:\n result = 10 / 0 # This will raise a ZeroDivisionError\nexcept Exception as e:\n print(str(e))”

Invoke Python Method activity:

  • Input - In - Variable: pythonScript
  • Output - Out - Variable: pythonOutput

Assign activity:

  • Variable: exceptionMessage
  • Value: pythonOutput

Log Message activity:

  • Message: "Exception Message: " + exceptionMessage

Thanks & Cheers!!!

1 Like

i need to get a exception message from the python code
Refer this python code:
PythonCode.txt (1.1 KB)
i.e :exception Variable

Hi @Kartheek_Battu

My actual scenario is have to break the python script while getting the error in python code.

Thanks in advance

Wrap your Invoke Python activity inside a UiPath’s try-catch. If your script fails, control comes to the catch automatically.

1 Like

As per you Suggestion i have given a Try catch for the invoke python Activity but I’m


getting this error while running a script in UiPath studio.

Did the error occur in your Python script?

No in UiPath studio while running a python code with an error.

I meant, did the error occur again in your script?

You can try another way too: use try-catch in your python script and use a boolean flag that determines if the control comes to the catch block in your script.

1 Like

Tried this way but having previous screenshot and after click close the program will get this pipe broken
image_2023_12_21T09_02_15_560Z

Can you got any idea?

If you’re performing any read/write operations, can you check if you’re closing the connection as required?

1 Like

Didn’t use any read/write operations for my flows so far.

Any luck to solve my issue.

Check this thread:

Invoke Python Method: Pipe is broken - Help / Activities - UiPath Community Forum

1 Like