Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Challenge: Refactor Broken Error Handling | Safe File Operations and Debugging
C# Exceptions and Error Handling Practice

bookChallenge: Refactor Broken Error Handling

Aufgabe

Swipe to start coding

Refactor a method with tangled error handling so it becomes clear and maintainable. The previous code had multiple nested try-catch blocks, unclear exception handling, and did not validate its arguments properly.

  • Throw an ArgumentException if filePath is null, empty, or contains only whitespace.
  • Attempt to read all text from the file at filePath.
  • Print the file's content to the console if successful.
  • If the file does not exist, catch FileNotFoundException and print a message including the exception's message.
  • If access is denied, catch UnauthorizedAccessException and print a message including the exception's message.
  • If any other IO error occurs, catch IOException and print a message including the exception's message.

Lösung

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 6
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

close

bookChallenge: Refactor Broken Error Handling

Swipe um das Menü anzuzeigen

Aufgabe

Swipe to start coding

Refactor a method with tangled error handling so it becomes clear and maintainable. The previous code had multiple nested try-catch blocks, unclear exception handling, and did not validate its arguments properly.

  • Throw an ArgumentException if filePath is null, empty, or contains only whitespace.
  • Attempt to read all text from the file at filePath.
  • Print the file's content to the console if successful.
  • If the file does not exist, catch FileNotFoundException and print a message including the exception's message.
  • If access is denied, catch UnauthorizedAccessException and print a message including the exception's message.
  • If any other IO error occurs, catch IOException and print a message including the exception's message.

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 6
single

single

some-alt