Skip to content
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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

spliffli
Copy link

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.

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.
@spliffli spliffli changed the title Update Helpers.cs Update main/dotnet/DWXConnect/api/Helpers.tryReadFile method in the C# code so that it handles exceptions that may happen, without crashing the program. Mar 26, 2024
@spliffli spliffli changed the title Update main/dotnet/DWXConnect/api/Helpers.tryReadFile method in the C# code so that it handles exceptions that may happen, without crashing the program. Update dotnet/DWXConnect/api/Helpers.tryReadFile method in the C# code so that it handles exceptions that may happen, without crashing the program. Mar 26, 2024
@spliffli spliffli changed the title Update dotnet/DWXConnect/api/Helpers.tryReadFile method in the C# code so that it handles exceptions that may happen, without crashing the program. Updated dotnet/DWXConnect/api/Helpers.tryReadFile method in the C# code so that it handles exceptions that may happen, without crashing the program. Mar 26, 2024
Changed CreateEmptyFile method from public to private
@spliffli spliffli changed the title Updated dotnet/DWXConnect/api/Helpers.tryReadFile method in the C# code so that it handles exceptions that may happen, without crashing the program. Updated Helpers.cs tryReadFile method in the C# code so that it handles exceptions that may happen, without crashing the program. Mar 26, 2024
@spliffli spliffli changed the title Updated Helpers.cs tryReadFile method in the C# code so that it handles exceptions that may happen, without crashing the program. Updated Helpers.cs tryReadFile method so that it handles exceptions that may happen, without crashing the program. Mar 26, 2024
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
@spliffli
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant