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.

Solution: Implement basic error handling

Solution: Implement basic error handling - C++ Tutorial

From the course: Mastering C++: Exception Handling

Solution: Implement basic error handling

(upbeat music) - [Instructor] Welcome back, how did it go? Now let's check out my solution. First, I check the stream using the good method to see if there are any issues with it. If not file, and I call the good method. Now, file.good checks for various I/O errors, not just opening errors. If this method returns false, I will throw a runtime_error exception. Throw, runtime_error. With a custom text say, "Fail to open configuration file." Now, let's go to the main function where we invoke the readConfigFile function. Since the function may throw an exception, I'll embed this part in a try block. Next, I'll add the catch block to handle the exception. As recommended, I'll catch the exception by const reference. And finally, I will simply print out the exception description using the exception what method. Let's say, "Error." And let's call the exception's what method. And finally, let's exit by returning a non-zero value to indicate that an error occurred. And that's it. Simple enough,…

Contents