{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ch05-title",
   "metadata": {},
   "source": [
    "# Exceptions & Testing"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ch05-overview",
   "metadata": {},
   "source": [
    "Errors are inevitable in any program. This chapter teaches you to recover from runtime failures and find logic errors before they reach users.\n",
    "\n",
    "- Exception handling for runtime failures\n",
    "- Tracebacks as debugging evidence\n",
    "- Custom exceptions for clearer error reporting\n",
    "- Debugging techniques and logging to find and record problems\n",
    "- Tests as executable checks of program behavior\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0500-video",
   "metadata": {},
   "source": [
    "<h2>Chapter Video</h2>\n",
    "\n",
    "This overview introduces exception handling with try, except, else, and finally.\n",
    "\n",
    "```{raw} html\n",
    "<iframe width=\"100%\" height=\"400\" src=\"https://www.youtube-nocookie.com/embed/NIWwJbo-9_8\" title=\"Python Tutorial: Using Try/Except Blocks for Error Handling\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen referrerpolicy=\"strict-origin-when-cross-origin\"></iframe>\n",
    "```\n",
    "\n",
    "> *[Python Tutorial: Using Try/Except Blocks for Error Handling (10:34)](https://youtube.com/watch?v=NIWwJbo-9_8) by Corey Schafer*\n",
    ">\n",
    "> Hosted on YouTube; property of its original creator, embedded here for educational reference only."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ch05-goals",
   "metadata": {},
   "source": [
    "<h2>Learning Objectives</h2>\n",
    "\n",
    "By the end of this chapter, you will be able to:\n",
    "\n",
    "1. Distinguish between syntax errors, runtime errors, and semantic errors\n",
    "2. Read and interpret Python tracebacks to locate the source of a bug\n",
    "3. Handle exceptions gracefully with `try`, `except`, `else`, and `finally`\n",
    "4. Raise exceptions and define custom exception classes\n",
    "5. Track down bugs with print-statement debugging, assertions, and bisection, and record diagnostics with `logging`\n",
    "6. Write tests with `pytest`, `unittest`, and `doctest`, including tests that expect an exception\n",
    "7. Isolate code from dependencies with mocks, test many inputs with parametrization, and read a coverage report\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ch05-flow",
   "metadata": {},
   "source": [
    "<h2>Chapter Flow</h2>\n",
    "\n",
    "```{container} chapter-flow\n",
    "1. {doc}`0501-exceptions`\n",
    "2. {doc}`0502-unit-testing`\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ch05-glossary",
   "metadata": {},
   "source": [
    "<h2>Glossary</h2>\n",
    "\n",
    "| # | Term | Explanation |\n",
    "|---:|---|---|\n",
    "| 1 | Exception | An object that represents an error or unusual condition during program execution. |\n",
    "| 2 | Exception handling | Code that responds to exceptions so a program can recover or fail clearly. |\n",
    "| 3 | `try` block | The block of code where Python watches for exceptions. |\n",
    "| 4 | `except` block | The block that runs when a matching exception occurs. |\n",
    "| 5 | `else` block | The block that runs after a `try` block if no exception occurs. |\n",
    "| 6 | `finally` block | The block that runs whether or not an exception occurs. |\n",
    "| 7 | Traceback | A report showing the call path and location of an exception. |\n",
    "| 8 | Debugging | The process of finding and fixing problems in a program. |\n",
    "| 9 | Runtime error | An error that occurs while a program is running. |\n",
    "| 10 | Semantic error | A logic error where code runs but produces the wrong result. |\n",
    "| 11 | Raise | To deliberately signal an exception with the `raise` statement. |\n",
    "| 12 | Custom exception | An exception class defined for a program-specific error case. |\n",
    "| 13 | Logging | Recording events, warnings, and errors from a running program. |\n",
    "| 14 | Log level | A severity category for a log message, such as DEBUG, INFO, WARNING, ERROR, or CRITICAL. |\n",
    "| 15 | Unit test | A test that checks a small part of a program in isolation. |\n",
    "| 16 | Assertion | A check, written with `assert`, that a condition is true; you use it in tests and as an in-code developer check of your program's assumptions. |\n",
    "| 17 | Doctest | A test written in a docstring using interactive Python examples. |\n",
    "| 18 | Fixture | Setup data or resources used by tests. |\n",
    "| 19 | Mock | A replacement object used to isolate code from an external dependency. |\n",
    "| 20 | Test coverage | A measure of which lines or branches of code were exercised by tests. |\n",
    "| 21 | Syntax error | An error in code that violates Python's grammar, reported before any of the program runs. |\n",
    "| 22 | Bisection | A debugging technique that checks the program's state at a midpoint to find which half contains the bug, then repeats on that half. |\n",
    "| 23 | Parametrized test | A single test function that runs once for each set of inputs, written with `@pytest.mark.parametrize`. |\n",
    "| 24 | Dependency injection | A design in which a function receives the objects it depends on as arguments, so a test can pass in a stand-in. |\n",
    "| 25 | Fake | A simple stand-in object that returns canned data in place of a real dependency during a test. |\n",
    "| 26 | Integration test | A test that checks several units of a program working together. |\n",
    "| 27 | Test-driven development | A workflow in which you write the tests first, then write the code that makes them pass. |\n",
    "| 28 | Test discovery | The way `pytest` finds tests automatically by collecting `test_*.py` files and functions whose names start with `test_`. |\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0500-slides",
   "metadata": {},
   "source": [
    "<h2><a href=\"overview.html\" target=\"_blank\" style=\"color: var(--pst-color-link, #176de8);\">Chapter Overview Slides</a></h2>"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
