Try catch exception for file not found is not working

I am trying to use try-catch block for doing “FileNotFoundException”
I have given a wrong path inside read text file which is in the try block.
inside catch block i have System.IO.FileNotFoundException and log message

Now when I execute, its directly showing could not file path but not the log message.

Error
Read Text File: One or more errors occurred. (Could not find file ‘C:\Users\91987\Documents\UiPath\BlankProcess3\trcatch.txt’.) (Could not find file ‘C:\Users\91987\Documents\UiPath\BlankProcess3\trcatch.txt’.) (Could not find file ‘C:\Users\91987\Documents\UiPath\BlankProcess3\trcatch.txt’.)

RemoteException wrapping System.AggregateException: One or more errors occurred. (Could not find file ‘C:\Users\91987\Documents\UiPath\BlankProcess3\trcatch.txt’.) (Could not find file ‘C:\Users\91987\Documents\UiPath\BlankProcess3\trcatch.txt’.) (Could not find file ‘C:\Users\91987\Documents\UiPath\BlankProcess3\trcatch.txt’.) —> RemoteException wrapping System.IO.FileNotFoundException: Could not find file ‘C:\Users\91987\Documents\UiPath\BlankProcess3\trcatch.txt’.
at Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(String fullPath,
FileMode mode,
FileAccess access,
FileShare share,
FileOptions options)
at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath,
FileMode mode,
FileAccess access,
FileShare share,
FileOptions options,
Int64 preallocationSize,
Nullable1 unixCreateMode) at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable1 unixCreateMode)
at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(String path,
FileMode mode,
FileAccess access,
FileShare share,
FileOptions options,
Int64 preallocationSize,
Nullable1 unixCreateMode) at System.IO.StreamReader.ValidateArgsAndOpenPath(String path, Encoding encoding, Int32 bufferSize) at System.IO.File.ReadAllText(String path, Encoding encoding) at UiPath.Core.Activities.ReadTextFile.<>c__DisplayClass19_0.<ExecuteAsync>b__0() at UiPath.Core.Activities.Retry.Do[ResultType](Func1 action,
Int32 timeoutMS,
Int32 retryCount)
— End of inner exception stack trace —
at UiPath.Core.Activities.TaskAsyncCodeActivityWithResult1.EndExecute(AsyncCodeActivityContext context, IAsyncResult result) at System.Activities.AsyncCodeActivity1.System.Activities.IAsyncCodeActivity.FinishExecution(AsyncCodeActivityContext context,
IAsyncResult result)
at System.Activities.AsyncCodeActivity.CompleteAsyncCodeActivityData.CompleteAsyncCodeActivityWorkItem.Execute(ActivityExecutor executor,
BookmarkManager bookmarkManager)

@SMRITI_S,

Instead of relaying on the Read text file activity to check if file exists or not, use proactive approach to check if file exists or not using logic like this.

The approach you are trying is reactive approach which should be avoided when building a reliable bot.


Thanks for the update, I tried it but now am getting error in Throw

1 Like

Hi @SMRITI_S its not an error click on continue then it will go to catch section to handle the exceptions

@SMRITI_S

you are running in debug mode..just click on continue it would go to catch block then

in run mode it does. not pause and will continue automatically

cheers

@SMRITI_S,

That’s expected behavior. Just click on continue and the control will go to Catch section and you will get the desired log.

Thanks a lot, it works completely fine now!

@SMRITI_S

That’s great! Kindly close the thread by marking my answer as solution so it will be helpful to other members as well.

Using try catch to check for file existence is both overkill and not really a proer standard.

Why not use
If: file.exists(“test.txt”)
Then: Logmessage: “it exists”
Else: logmessage “it doesn’t”
End if

Creating error events for something so trivial is wastefull and makes code a lot less readable.

I understand, unfortunately we have a syllabus to follow in our college so we need to stick to the necessary activities

Great college if they teach incorrect methods :smiley:
But I understand… it’s about the exercise.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.