From the course: Debugging in C++ with Visual Studio Code
Using assertions - Visual Studio Code Tutorial
From the course: Debugging in C++ with Visual Studio Code
Using assertions
- [Instructor] Another useful technique to proactively help developers in the debugging process is using assertions. Assertions are a debugging tool, not a production feature. They help us verify that certain conditions are true while the program is running. If an assertion fails, the program stops immediately with a clear error message making it obvious where things went wrong. Let's look at our calculator again. In this version, I have added two assertions to check for the same breaking cases we've already covered. As you can see in line seven, we are including the cassert header file. First, look at the error message for invalid input format in line 22. This doesn't use an assertion. Instead, we handle it with an error message. The reason for this is that assertions should only check conditions that must be true during normal execution. Here the input comes from the user, which means it's expected to fail sometimes. Someone might type hello instead of a number or forget to include an operator. Assertions are not meant for handling expected failures like user input errors. No, they are meant to catch programming mistakes. That's why we handle this case with an error message and continue instead of using assert. Now, let's look at the first assertion in the program in line 26. This checks that the OP variable is one of the four valid operators, addition, subtraction, multiplication, or division. If it's not, the assertion fails and the program stops. Notice how I'm including the error message with a Boolean and operator. That's a classic workaround to the fact that the cassert library doesn't have a mechanism to print an error message when an assertion fails. The assertion just fails and shows its condition. So the and operation makes the string neutral to the Boolean expression and the assertion failure message will show the whole expression, including the error message. The second assertion is in line 29. This one checks for division by zero. If the user enters a division operation where num two is zero, the assertion triggers immediately and prevents the program from continuing. Now, here's the key thing about assertions. They only work in debugging mode. If you compile your code without debugging, assertions are completely removed from the program. That means they are not a replacement for proper error handling. They are just a tool to catch bugs early during development. So, should we use assertions in production code? No. But while debugging, they are an invaluable way to enforce rules and catch problems before they turn into real errors. Now, let's run the program and see these assertions in action. First, I'll enter an invalid string. There's the error message and the program continues. Now let me try an invalid operator. Here we can see two things. First, we have the alarming red message showing an exception called aborted. Don't worry about this one. This is just the debugger telling us about an exception. The second thing we see is in the terminal. The program ended and we see a message indicating that the assertion failed. Notice that our message is being shown as part of the expression. There you go. Assertions are a great way to detect inaccuracies in your code during the testing and debugging stages of the development process.