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

bookChallenge: Refactor Broken Error Handling

Tâche

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.

Solution

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 4. Chapitre 6
single

single

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

close

bookChallenge: Refactor Broken Error Handling

Glissez pour afficher le menu

Tâche

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.

Solution

Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 4. Chapitre 6
single

single

some-alt