Concept Explanation
In this lesson, you practice debugging a small C++ program that fails in a predictable way. The point is not just to see an error message. The point is to follow the flow: identify what the code is trying to do, reproduce the issue, and then improve the program so the behavior is easier to understand. This is a beginner-friendly debugging habit that will matter later when your programs become larger and the source of a problem is less obvious.
Where to Put the Code
- Start with variables and inputs. Keep includes, main function, and data types explicit.
- Add processing logic in the middle section.
- Finish with output and quick validation.
Command Reference
- Run the program and confirm which input causes the failure path.
- Change the values in `divideItems` to test one working case and one failing case.
- Keep the throw site and the catch block easy to read so the error path is obvious.
- Describe one debugging choice that made the program easier to inspect.
Step-by-step Guide
- Build and run the example exactly as written to observe the handled error.
- Identify why `divideItems(12, 0)` cannot succeed.
- Change the second argument to a valid number and compare the new output.
- Add or edit one message so the failure case is clearer to a user reading the terminal.
- Summarize in one sentence how the `try` and `catch` blocks help during debugging.
Practice Exercises
- Add a second test call with a valid divisor and print both results clearly.
- Rewrite the example so the function checks for another bad input such as negative boxes.
- Create a small report in `main` that shows whether each test case passed or failed.
Coding Challenges
- Implement the same scenario once with direct checks and once with an exception, then compare readability.
- Refactor the code so the calculation logic and the user-facing messages are separated cleanly.
Mini Practice Tasks
- Rename one variable so the intention of the code is easier to follow.
- Add one extra guarded case and rerun the program.
- Write a one-line summary of the bug this lesson is designed to expose.
Common Mistake
Skipping input validation or mixing logic/output in one unstructured block.
Real-life Mini Challenge
Build a small real-life example for this lesson topic using 3 clear steps: input, process, output.