MORE: C# Exception Handling Best Practices How to Catch Unhandled Exceptions in C# This code can easily throw several types of exceptions and lacks exception handling best practices. String fileContents = File.ReadAllText(args) This code will throw exceptions if no file path is passed in or the file does not exist. The code then loads the contents of the file path being passed in. The developer is assuming that within “args” a valid file path will be passed in. This is a simple example of a potential known problem that is accounted for within the code.Īn unhandled exception occurs when a developer does not anticipate and handle a potential exception NET Framework will then throw a FileNotFoundException. An unhandled exception occurs when the application code does not properly handle exceptions.įor example, When you try to open a file on disk, it is a common problem for the file to not exist.