Concepts / Understanding Error Messages in Python

Understanding Error Messages in Python

but that is much uglier and error-prone. Second, the conversion to string would be done automatically by the format method instead of the explicit conversion to strings needed in this case. Third, when using the format method, we can change the message without having to deal with the variables used and vice-versa.

  • 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.

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.

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.

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

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

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

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

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.

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

Why format Is Easier to Maintain

Changing the wording without changing the values

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

Start with one template: The message template describes the requirement and contains positions for length and atleast.

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

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.

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

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

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

  • Treating the default error message as the only possible response.

    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.

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

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

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

    Fix: Use format so conversion to strings happens automatically.

  • Mixing message wording directly into the handling of every variable.

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

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

  • Using assert automatically whenever a problem occurs.

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

    Fix: 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.