From the course: Secure Coding in C++
Unlock this course with a free trial
Join today to access over 25,500 courses taught by industry experts.
Writing secure error handling code - C++ Tutorial
From the course: Secure Coding in C++
Writing secure error handling code
- [Instructor] Here we have a program that calls two functions that may throw exceptions. In lines 10 and 15 we have these functions respectively. One simulates processing a string and the other one loads a file. Both throw exceptions with a message to report what went wrong. The first, when the input is empty, and the other one when the file cannot be opened. In the main function, we call those two functions in a try statement, starting at line 22. Now look at line 26. The problem is in the catch block. We are catching everything but doing nothing. This is a bad practice that causes silent failures, one of the worst kinds of failures. If we run this program, it will silently fail. Let me show you. Nothing. We don't see any messages. We don't know what happened. Did it succeed? Did it fail? We don't know. So let's fix that. First, let me specify that we are catching standard exceptions in line 26. Then let's print the actual error message so we know what went wrong. That's it. Let's…