-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updated Helpers.cs tryReadFile method so that it handles exceptions that may happen, without crashing the program. #44
base: main
Are you sure you want to change the base?
Conversation
Updated the tryReadFile method so that it handles some exceptions and I ran into all 3 of these exceptions while building upon this code in another project and thought it might be worth contributing. Because the tryReadFile method frequently gets called from a loop for checking for a file at a given path, returning an empty string after handling each exception means it will just try again at the next loop and operate smoothly without crashing the program. - DirectoryNotFoundException can happen when the MetaTrader EA either isn't running or isn't initialized properly, and it returns an empty string to continue the loop that's constantly checking a filepath until it either works or possibly it can be updated to infer the status of the EA as offline which could be useful. - FileNotFoundException was happening in my program even when the EA was running, and I found that by creating an empty file at location where it's expected to be fixes the problem, so it does that and returns an empty string. - IOException happens when more than one process accesses the same resource at the exact same time a.k.a. a race condition. This can be handled by just returning an empty string.
Changed CreateEmptyFile method from public to private
Added a private method GetFileNameFromPath which returns the filename at the end of a filepath. This is used for printing the filename when printing the exception for clarity's sake.
Added missing $ symbol before string
By the way, sorry for doing so many commits after I opened the pull request. It's because as I was editing the file in this repo directly on github, I improvised a new method to get the file name from a filepath so it can write that to the console when there's an exception, and I forgot to add the var keyword. Feel free to edit the Console.WriteLine statements since I know you use a helper method called print for writing to the console. Also, if you want me to instead open a different pull request on the developer branch instead of the main branch I can do that. |
Updated the tryReadFile method so that it handles some exceptions and I ran into all 3 of these exceptions while building upon this code in another project and thought it might be worth contributing.
Because the tryReadFile method frequently gets called from a loop for checking for a file at a given path, returning an empty string after handling each exception means it will just try again at the next loop and operate smoothly without crashing the program.
DirectoryNotFoundException can happen when the MetaTrader EA either isn't running or isn't initialized properly, and it returns an empty string to continue the loop that's constantly checking a filepath until it either works or possibly it can be updated to infer the status of the EA as offline which could be useful.
FileNotFoundException was happening in my program even when the EA was running, and I found that by creating an empty file at location where it's expected to be fixes the problem, so it does that and returns an empty string.
IOException happens when more than one process accesses the same resource at the exact same time a.k.a. a race condition. This can be handled by just returning an empty string.