From the course: Mastering C++: Exception Handling

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Challenge: Implement basic error handling

Challenge: Implement basic error handling - C++ Tutorial

From the course: Mastering C++: Exception Handling

Challenge: Implement basic error handling

(upbeat music) - [Instructor] Suppose we have a program that needs to read the configuration file at startup. This file is crucial for the program and its absence or inaccessibility is unexpected and happens rarely. The code uses the read config file function, which takes a file name as input and returns a string containing the text fetched from the file. In the main function, we call the read config file function and simply print out the text read from the input file. Now, file system operations can fail due to various reasons. The file may not exist in the first place. There might be permissions restrictions in place, or its contents could be corrupted and so on. The current code is not prepared for any such surprises. Your task is to enhance the program by adding the missing error handling. Here are some hints. Use the stream's good method to check if the stream associated with the file in the read config file function is good to go. If the stream has no issues, proceed with…

Contents