Challenge: Refactor Broken Error Handling
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
ArgumentExceptioniffilePathis 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
FileNotFoundExceptionand print a message including the exception's message. - If access is denied, catch
UnauthorizedAccessExceptionand print a message including the exception's message. - If any other IO error occurs, catch
IOExceptionand print a message including the exception's message.
Løsning
Takk for tilbakemeldingene dine!
single
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Can you explain this in simpler terms?
What are the main takeaways from this?
Can you give me an example?
Fantastisk!
Completion rate forbedret til 4.17
Challenge: Refactor Broken Error Handling
Sveip for å vise menyen
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
ArgumentExceptioniffilePathis 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
FileNotFoundExceptionand print a message including the exception's message. - If access is denied, catch
UnauthorizedAccessExceptionand print a message including the exception's message. - If any other IO error occurs, catch
IOExceptionand print a message including the exception's message.
Løsning
Takk for tilbakemeldingene dine!
single