Refactoring for Better Error Handling
When working with error handling in C#, the way you structure your try-catch blocks can have a big impact on how easy your code is to read, maintain, and debug. Refactoring means improving the structure of your code without changing its external behavior. This is especially important for error handling, where messy or unclear code can make it hard to spot bugs or understand what happens when something goes wrong. By learning to refactor your error handling, you make your programs safer and easier for others (and your future self) to work with.
Program.cs
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364using System; using System.IO; namespace ConsoleApp { public class Program { public static void Main(string[] args) { // Messy try-catch block try { string path = "data.txt"; string content = File.ReadAllText(path); Console.WriteLine("File content: " + content); } catch (FileNotFoundException ex) { Console.WriteLine("File not found: " + ex.Message); } catch (IOException ex) { Console.WriteLine("IO error: " + ex.Message); } catch (Exception ex) { Console.WriteLine("Something went wrong: " + ex.Message); } // Refactored for clarity try { PrintFileContent("data.txt"); } catch (Exception ex) { HandleFileError(ex); } } public static void PrintFileContent(string path) { string content = File.ReadAllText(path); Console.WriteLine("File content: " + content); } public static void HandleFileError(Exception ex) { if (ex is FileNotFoundException) { Console.WriteLine("File not found: " + ex.Message); } else if (ex is IOException) { Console.WriteLine("IO error: " + ex.Message); } else { Console.WriteLine("Something went wrong: " + ex.Message); } } } }
In the messy version, all file reading and error handling are mixed together in one place. This makes the code hard to follow and more difficult to update. In the refactored version, error handling is separated into its own method, and file reading logic is also extracted. This clear separation helps you quickly see where errors are handled and where the main logic lives, making the code much easier to maintain and extend.
ExtractedErrorHandling.cs
1234567891011121314151617181920212223// This is a non-runnable example showing how you might extract error handling into methods public void ProcessFile(string path) { try { ReadAndPrintFile(path); } catch (Exception ex) { LogAndReportError(ex); } } private void ReadAndPrintFile(string path) { // Logic to read and print file } private void LogAndReportError(Exception ex) { // Logic to log the error and report to user }
1. What is refactoring?
2. How does refactoring improve error handling?
Merci pour vos commentaires !
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Génial!
Completion taux amélioré à 4.17
Refactoring for Better Error Handling
Glissez pour afficher le menu
When working with error handling in C#, the way you structure your try-catch blocks can have a big impact on how easy your code is to read, maintain, and debug. Refactoring means improving the structure of your code without changing its external behavior. This is especially important for error handling, where messy or unclear code can make it hard to spot bugs or understand what happens when something goes wrong. By learning to refactor your error handling, you make your programs safer and easier for others (and your future self) to work with.
Program.cs
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364using System; using System.IO; namespace ConsoleApp { public class Program { public static void Main(string[] args) { // Messy try-catch block try { string path = "data.txt"; string content = File.ReadAllText(path); Console.WriteLine("File content: " + content); } catch (FileNotFoundException ex) { Console.WriteLine("File not found: " + ex.Message); } catch (IOException ex) { Console.WriteLine("IO error: " + ex.Message); } catch (Exception ex) { Console.WriteLine("Something went wrong: " + ex.Message); } // Refactored for clarity try { PrintFileContent("data.txt"); } catch (Exception ex) { HandleFileError(ex); } } public static void PrintFileContent(string path) { string content = File.ReadAllText(path); Console.WriteLine("File content: " + content); } public static void HandleFileError(Exception ex) { if (ex is FileNotFoundException) { Console.WriteLine("File not found: " + ex.Message); } else if (ex is IOException) { Console.WriteLine("IO error: " + ex.Message); } else { Console.WriteLine("Something went wrong: " + ex.Message); } } } }
In the messy version, all file reading and error handling are mixed together in one place. This makes the code hard to follow and more difficult to update. In the refactored version, error handling is separated into its own method, and file reading logic is also extracted. This clear separation helps you quickly see where errors are handled and where the main logic lives, making the code much easier to maintain and extend.
ExtractedErrorHandling.cs
1234567891011121314151617181920212223// This is a non-runnable example showing how you might extract error handling into methods public void ProcessFile(string path) { try { ReadAndPrintFile(path); } catch (Exception ex) { LogAndReportError(ex); } } private void ReadAndPrintFile(string path) { // Logic to read and print file } private void LogAndReportError(Exception ex) { // Logic to log the error and report to user }
1. What is refactoring?
2. How does refactoring improve error handling?
Merci pour vos commentaires !