Concepts / Understanding Error Messages in Python

Understanding Error Messages in Python

A useful error message tells the user what went wrong by combining appropriate wording with the relevant values from the problem.

  • Programming

When Execution Stops

An error message is not merely text printed at the end of a failed program. It is information about a problem that interrupted execution. If an error or exception is not handled, Python's default handler stops the program and prints an error message. Understanding how that message is produced and how its wording is assembled makes it easier to communicate problems clearly.

Key point

A useful error message tells the user what went wrong by combining appropriate wording with the relevant values from the problem.

From Exception to Message

There are two related situations to keep separate. In the first, an exception is not handled. Python's default handler stops execution and prints an error message. In the second, a program catches the exception. In an except clause, the program names the class of error and stores the corresponding error or exception object in a variable. Code inside that except clause can then use information from the exception object to print an appropriate message to the user.

Worked example

Reading the message-building process

A caught exception provides values such as length and atleast. The program needs to communicate those values in a message.

  1. 1
    Identify the problem

    The exception object contains information that can help explain the problem to the user.

  2. 2
    Choose the wording

    The program creates a message template describing what the user needs to know.

  3. 3
    Insert the values

    The values from the exception object are placed into the appropriate positions in the message.

  4. 4
    Display the result

    The except clause prints the resulting message instead of leaving the user with no explanation beyond the failure.

Result

A handled exception can produce a message that combines fixed explanatory text with values associated with the exception.

Template, Placeholders, and Values

The format method separates the wording of a message from the variables that supply its changing values. The message contains a template with placeholders. The variables provide the values that belong in those positions. The format method combines them into the final message and automatically converts the inserted values to strings. This avoids the explicit string conversions required by manual concatenation.

Message templatefixed wording withplaceholdersFormatted messagewording with valuesinsertedlengthvariable valueatleastvariable valueprovides wording and positionsfills a placeholderfills a placeholder
How do the message template, placeholders, and variable values combine to produce the final error message?

Why format Is Easier to Maintain

Worked example

Changing the wording without changing the values

Suppose a message uses the variables length and atleast to explain a requirement.

  1. 1
    Start with one template

    The message template describes the requirement and contains positions for length and atleast.

  2. 2
    Change only the wording

    The program can revise the explanation for the user while continuing to use the same length and atleast variables.

  3. 3
    Keep the values independent

    The values remain data supplied by the exception object; they do not have to be embedded into a newly rewritten sentence.

  4. 4
    Produce the final message

    format combines the revised template with the existing values and performs the needed conversion to strings automatically.

Result

Separating the template from its values makes message changes less error-prone than manually assembling the entire message.

ApproachString conversionMaintenance
Manual concatenationValues require explicit conversion to stringsThe assembled message can become uglier and more error-prone
format methodConversion to strings happens automaticallyMessage wording and variables can be changed separately

The source contrasts manual string assembly with the format method.

Handling Errors Responsibly

A message should be part of a deliberate response to a problem. When an exception is caught, the except clause can use the exception object and display an appropriate explanation to the user. The source also advises using assert judiciously. Most of the time, it is better to catch exceptions, handle the problem or display an error message to the user, and then quit.

Mistakes with Error Messages

  • Common mistake

    Treating the default error message as the only possible response.

    What it looks like

    An unhandled exception is allowed to reach Python's default handler even though the program could explain the problem more appropriately.

    Why it fails

    The default handler stops execution and prints a message, but it does not replace a deliberate user-facing response when the program needs to explain the problem.

    Do this instead

    Catch the relevant exception when the program needs to handle the problem or display an appropriate message.

  • Common mistake

    Manually converting every inserted value to a string when using a formatted message.

    What it looks like

    The programmer explicitly converts values before assembling the message.

    Why it fails

    This makes the message-building expression uglier and more error-prone than necessary.

    Do this instead

    Use format so conversion to strings happens automatically.

  • Common mistake

    Mixing message wording directly into the handling of every variable.

    What it looks like

    Changing a sentence requires rewriting each part that inserts the variables.

    Why it fails

    The wording and the variables become unnecessarily dependent on each other.

    Do this instead

    Keep the message template separate from the values supplied by the exception object.

  • Common mistake

    Using assert automatically whenever a problem occurs.

    What it looks like

    A user-facing problem is handled only through an assertion.

    Why it fails

    The source recommends using assert judiciously and generally favors catching exceptions, handling the problem or displaying a message, and then quitting.

    Do this instead

    Consider exception handling for problems that need an explicit response.

Message Design Practice

MEDIUM

Imagine that an except clause has an exception object containing the values length and atleast. Describe how you would design a user-facing error message using a template and the format method. Then explain which part you would change if you wanted to revise the wording and which part you would change if the exception supplied different values.

Hints
  • Separate fixed message wording from the values supplied by the exception object.
  • Remember that format performs the conversion to strings automatically.
  • Consider why this approach is less error-prone than manual string concatenation.

Key Takeaways

  • An unhandled error or exception causes Python's default handler to stop execution and print an error message.
  • An except clause can store the corresponding exception object in a variable and use its information to create an appropriate message.
  • The format method combines a message template with variable values and converts those values to strings automatically.
  • Keeping message wording separate from variables makes error messages easier to change and less error-prone to assemble.
  • Use assert judiciously; for many problems, catch the exception, respond appropriately, and then quit.

More concepts starting with U