Try-Catch Error

Hi

I have never used a Try-Catch before. I want to read some txt. files, but some may not exist. If file exist, the robot will extract some text from file and write the text to an Excel file.

I did a

Try : Read txt file
Catch Exception: “Syste.Aggregate Exception” then write a line that “File not found.”
Finally: Write extracted text to Excel

When I get Catch exception “File not found.”, it still move to Finally and write the previously extracted text into Excel.
I don’t to write any text into Excel if “File not found”.
Thank you

Hey @Anonymous2,

I think an easier way (if all the text files are in same folder) is get all the files and do a For Each on them, that way if a file is missing, it won’t be expected by the Workflow and you should not have the problem.

Cheers,
MikeB

@Anonymous2 Haii… Ensure that u have ok with ur RE framework… It May differ fr high level and low level exception

Hi @Anonymous2

Change your process like this.

Try : Read txt file and Write extracted text to Excel
Catch Exception: “Syste.Aggregate Exception” then write a line that “File not found.”
Finally: Nothing

Because the content in Finally will always be executed, whether exception occurred or not.

While answer from @wusiyangjia (#4) should solve your issue, it is not recommended to use Try Catch when you can check if the file exists before trying to read the file.

@wusiyangjia @KannanSuresh Both of your comments make sense.
@Anonymous2 For this specific issue, you should add activity to “Catch” section instead of “Finally”.
The bot will run activity in “Finally”. It doesn’t matter you encounter exception or not.

When I get Catch exception “File not found.”, it still move to Finally and write the previously extracted text into Excel.