© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
9 replies
kask

✅ Reducing exception usage, async methods

I've read a lot of using exceptions, best practices and such. As quick summary, the exceptions should be used on unexpected situations only (config errors, db connection errors..), not as validation errors and such. I understood, that for example,
byte[] GetFileBytes(string filePath)
byte[] GetFileBytes(string filePath)
shouldn't throw if file doesn't exist.

I'm refactoring some my old projects as practice. For synchronous methods I've started using some TryGet pattern and it works great. But what about async methods? Async methods can't have out parameters.

If we look at my earlier example and convert it into async method
async Task<byte[]> GetFileBytes(string filePath)
async Task<byte[]> GetFileBytes(string filePath)
, should I convert it to synchronous method or use some other pattern? Or should I throw a custom exception and attach original exception as inner? Catching all exceptions in multiple services and methods doesn't seem to be efficient (
catch (Exception e) when (e is PathTooLongException or DirectoryNotFoundException or IOException or UnauthorizedAccessException or FileNotFoundException or SecurityException)
catch (Exception e) when (e is PathTooLongException or DirectoryNotFoundException or IOException or UnauthorizedAccessException or FileNotFoundException or SecurityException)
)
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ How do async methods handle references
C#CC# / help
2y ago
Chaining async methods. Method not found
C#CC# / help
3y ago
✅ How do I run async methods? WPF
C#CC# / help
9mo ago
❔ Reducing commands
C#CC# / help
3y ago