PNL Learn

Errors and Exceptions

Read tracebacks, handle failure and raise errors that help.

Every real program meets bad input, missing data and failed operations. You will read multi-frame and chained tracebacks like a working developer, catch exactly the exceptions you expect with try, except, else and finally, and use the exception hierarchy to catch whole families while leaving SystemExit and Ctrl+C alone. You will raise errors with messages that point straight at the problem, re-raise and chain causes, define your own exception types that carry data, and choose between EAFP, LBYL and assert. The course ends with a log scanner, a config parser, a bank, retry helpers and a validator that report every problem instead of crashing on the first.

IntermediatePython6 lessons72 exercisesAbout 5.5 h
Create a free account

What you will learn

  • Tracebacks
  • Debugging
  • try and except
  • The exception hierarchy
  • Raising and chaining
  • Custom exceptions
  • EAFP and assert
  • Retries

Lessons

  1. 1

    Reading Tracebacks

    Read an error from the bottom up, name the exception, and track down where the bug really lives.

    13 exercises

  2. 2

    try and except

    Catch the exceptions you expect, keep the ones you do not, and clean up no matter what.

    13 exercises

  3. 3

    The Exception Hierarchy

    Catch whole families of errors with a parent class, leave exits alone, order your except clauses, and handle errors where you can act on them.

    10 exercises

  4. 4

    Raising Exceptions

    Raise errors with messages that point at the problem, validate arguments, read an exception's args, re-raise and chain causes.

    13 exercises

  5. 5

    Custom Exceptions and EAFP

    Define your own kinds of error, give them data, catch whole families of them, and choose between EAFP, LBYL and assert.

    14 exercises

  6. 6

    Putting It Together

    Build a log scanner, a config parser, a bank, a report, retry helpers and a validator that fail well.

    9 exercises

How you practice

You practice in the browser and every exercise gives you feedback right away. This course uses these formats:

  • Code exercise: 23
  • Fix the bug: 15
  • Type the answer: 8
  • Predict the output: 7
  • Multiple choice: 6
  • Fill in the blank: 6
  • Spot the bug: 3
  • Match the pairs: 2
  • Select all that apply: 2

Aligned to

PCEP-30-02Python InstituteSource
  • Functions and Exceptions
    • 4.3Python Built-In Exceptions Hierarchy
    • 4.4Basics of Python Exception Handling
PCAP-31-03Python InstituteSource
  • Exceptions
    • 2.1Handle errors using Python-defined exceptions
    • 2.2Extend the exceptions hierarchy with self-defined exceptions
CS2023ACM / IEEE-CS / AAAISource
  • Software Development Fundamentals
    • SDF-FundamentalsFundamental Programming Concepts and Practices

Proto Node Labs is not affiliated with or endorsed by Python Institute and ACM / IEEE-CS / AAAI. Exam names are trademarks of their owners.