Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære 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

Oppgave

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øsning

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 6
single

single

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

Can you explain this in simpler terms?

What are the main takeaways from this?

Can you give me an example?

close

bookChallenge: Refactor Broken Error Handling

Sveip for å vise menyen

Oppgave

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øsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 6
single

single

some-alt