Awesome!
Completion rate improved to 4.17Section 4. Chapter 2
single
Challenge: File Reading with Error Handling
Swipe to show menu
Task
Swipe to start coding
Implement a method that reads the contents of a file at the given filePath. If the file does not exist, return "Error: File not found.". If there is an input/output error while reading the file, return "Error: Unable to read the file.". If the file is read successfully, return its contents as a string.
- Attempt to read the contents of the file at
filePath. - If a
FileNotFoundExceptionoccurs, return"Error: File not found.". - If an
IOExceptionoccurs, return"Error: Unable to read the file.". - If the file is read successfully, return its contents.
Solution
Everything was clear?
Thanks for your feedback!
Section 4. Chapter 2
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat