Lab 4: Debugging and Exception Handling
Instructions
-
First read this page then start working on lab with the GitHub classroom link below.
-
Put your code in the GitHub repository for this lab.
-
After you complete the lab, answer the questions in the README.md file. Enter your answers directly in the README.md file.
-
Github Classroom Link: https://classroom.github.com/a/O7kEXDG9
Objective
Familiarize yourself with the essential features of the VSCode Debugger while working with a C++ programs. Learn and gain experience with errors and employ try
/catch
for exception handling.
Part 1 - Debugging
- Setup:
- Open the file
ecommerce.cpp
in thePart1
directory in the repository.
- Open the file
- Setting Breakpoints:
- Set a breakpoint at the start of the
main
function. - Set another breakpoint inside the
processOrder
function. - Add a third breakpoint in the
applyDiscount
function.
- Set a breakpoint at the start of the
- Start Debugging:
- Launch the debugger. First click the debug icon in the activity bar on the left. Then click the “Run and Debug” button.
- Stepping Through the Code:
- When the debugger pauses at main, press “Step Over” until you get to the
processOrder
function call. - Now, press “Step Into” to dive into the
processOrder
function. - Inside the
processOrder
function, you’ll soon see theapplyDiscount
function call. First, press “Step Over” to notice how you skip the internals ofapplyDiscount
and directly get its return value. - Run the
processOrder
function again. This time, when you’re atapplyDiscount
, press “Step Into” to go inside it and inspect its inner workings.
- When the debugger pauses at main, press “Step Over” until you get to the
- Observing the Call Stack:
- Inside the
applyDiscount
function, take a moment to observe the CALL STACK pane. You should see the function hierarchy:applyDiscount
called fromprocessOrder
which was called frommain
.
- Inside the
- Modifying Variables & Stepping Out:
- In the
applyDiscount
function step over each line until you reach thereturn discount
line. - In the
applyDiscount
function, under the VARIABLES section of the debugger, change the value of discount to20
. - After making this change, press “Step Out” to move back to the
processOrder
function and observe how the change impacts the total cost.
- In the
- Completing Execution:
- Press “Continue” to complete the program.
- Expected Output with discount set to 10:
Total Cost for John Doe: $50.99
- Expected Output with discount set to 20:
Total Cost for John Doe: $45.99
Part 2 - More Debugging
- Setup:
- Open the file
factorial.cpp
in thePart2
directory in the repository.
- Open the file
- Setting Breakpoints:
- Set a breakpoint inside the factorial function.
- Start Debugging:
- Launch the debugger.
- As the debugger stops at the breakpoint:
- Inspect Variables: Hover over the variable n to observe its value during each recursive call.
- Step Through the Code: Use the step-over and step-into buttons to navigate through the recursive calls.
- Try to identify the reason why the recursion never terminates.
- Fixing the Recursive Function:
- Once you’ve identified the missing base case, modify the factorial function to include a termination condition for the recursion.
- Debug the program again to ensure the recursive function now works correctly.
- Expected Output:
Factorial of 10 is: 3628800
- Now try:
- Modifying the input value (number) to larger values and see how high you can go before running into potential issues.
- Observe how the call stack grows with larger input values.
- Introduce other recursive functions, like the Fibonacci sequence, and practice debugging them.
Part 3 - Exception Handling
Your task is to enhance the factorial.cpp
program in the Part3
directory to handle potential errors gracefully. Here are the steps:
-
Check for Negative Inputs: The factorial function is only defined for non-negative integers. Modify the factorial function to check if the input number n is negative. If it is, throw an appropriate exception.
-
Handle Potential Overflow: The result of the factorial calculation can grow rapidly for even relatively small input values. Before each multiplication, check if the next multiplication would cause an overflow. If an overflow is detected, throw an appropriate exception. Hint: You might find numeric_limits
::max() useful for this check.
if (result > numeric_limits<unsigned long long>::max() / i)
{
throw overflow_error("Result will overflow.");
}
-
Catch Exceptions in the
main
Function: In themain
function, use a try-catch block to catch any exceptions thrown from the factorial function. Display an appropriate error message to the user if an exception is caught. -
Test Your Program: Run your program and test it with various input values, including negative numbers and numbers that might cause an overflow, to ensure that your error handling works as expected.
-
Remember: Proper error handling is essential for building robust and user-friendly software. Take this opportunity to practice identifying potential errors in code and handling them gracefully!
After you complete the lab, answer the questions in the README.md file. Enter your answers directly in the README.md file.