Testing, Debugging, and Error Handling

6. Testing, Debugging, and Error Handling#

An invalid quantity, a failing file operation, and a wrong total call for different responses. This chapter brings the earlier checking habits together into validation, intentional exception handling, evidence-based debugging, and repeatable unit tests. You will distinguish reporting an expected failure from investigating a defect and verify that a repair preserves working cases.

  • Input validation: Checking values before risky operations prevents many avoidable failures.

  • Exception handling: try, catch, and related constructs let a program respond intentionally to runtime errors.

  • Debugging: Breakpoints, watches, and step-by-step execution help you inspect what a program is actually doing.

  • Unit testing: Small repeatable tests build confidence that code keeps working as it changes.

  • Missing input: Basic null checks distinguish absent input from valid values. Deeper nullable-type and object-contract discussion follows in Chapter 11.

Chapter Video

C# exception handling ⚠️ (5:01) by Bro Code.

Hosted on YouTube; property of its original creator, embedded here for educational reference only.

Learning Objectives

By the end of this chapter, you will be able to:

  1. Choose validation or exception handling for a stated failure and use TryParse for expected invalid input.

  2. Trace try, matching catch, and finally paths and report or throw failures intentionally.

  3. Reproduce a defect, form a hypothesis, and use breakpoints, watches, stepping, and the call stack to test it.

  4. Write repeatable method tests with explicit expected results and meaningful normal, boundary, and failure cases, including unchanged state after rejection.

  5. Verify a repair with regression checks and evaluate whether proposed tests challenge the actual contract.

Chapter Flow

Supporting reading in OOP: Nullability and Missing Data.

Glossary

#

Term

Explanation

1

Exception

A runtime event that interrupts normal program flow.

2

Runtime error

An error that appears while the program is running.

3

Try block

A block containing code that might throw an exception.

4

Catch block

A block that handles a specific exception.

5

Finally block

A block that runs after try and catch, whether or not an exception occurred.

6

Throw

To signal an exception from code.

7

Validation

Checking data before using it in an operation that has requirements.

8

Debugger

A tool for pausing and inspecting a running program.

9

Breakpoint

A marked line where the debugger pauses execution.

10

Step over

A debugging action that runs the current line without entering called methods.

11

Unit test

A small automated check for one behavior.

12

Test case

One input and expected-result scenario.

13

Null check

A condition that verifies a reference is not null before use.

14

Regression test

A repeatable check that previously working behavior still works after a change.

15

Reproduction

A repeatable sequence of inputs and actions that exposes a failure.

16

Hypothesis

A proposed explanation of a defect that can be tested with execution evidence.

17

Call stack

The active chain of method calls available for inspecting a failure.

18

TryParse

A parsing pattern that reports success as a Boolean and supplies a converted result without throwing for expected invalid text.

19

Test isolation

Giving each test its own required starting conditions so test order does not determine its result.

20

Repeatability

Obtaining the same test verdict from unchanged code and controlled inputs.

Lecture Slides

Chapter 6 lecture slides follow the current reading order. Supporting parts remain optional; instructor notes and editable decks are distributed separately.