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.
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.
- 1Identify the problem
The exception object contains information that can help explain the problem to the user.
- 2Choose the wording
The program creates a message template describing what the user needs to know.
- 3Insert the values
The values from the exception object are placed into the appropriate positions in the message.
- 4Display 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.
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.
- 1Start with one template
The message template describes the requirement and contains positions for length and atleast.
- 2Change only the wording
The program can revise the explanation for the user while continuing to use the same length and atleast variables.
- 3Keep the values independent
The values remain data supplied by the exception object; they do not have to be embedded into a newly rewritten sentence.
- 4Produce 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.
| Approach | String conversion | Maintenance |
|---|---|---|
| Manual concatenation | Values require explicit conversion to strings | The assembled message can become uglier and more error-prone |
| format method | Conversion to strings happens automatically | Message 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 likeAn unhandled exception is allowed to reach Python's default handler even though the program could explain the problem more appropriately.
Why it failsThe 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 insteadCatch 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 likeThe programmer explicitly converts values before assembling the message.
Why it failsThis makes the message-building expression uglier and more error-prone than necessary.
Do this insteadUse format so conversion to strings happens automatically.
Common mistake Mixing message wording directly into the handling of every variable.
What it looks likeChanging a sentence requires rewriting each part that inserts the variables.
Why it failsThe wording and the variables become unnecessarily dependent on each other.
Do this insteadKeep the message template separate from the values supplied by the exception object.
Common mistake Using assert automatically whenever a problem occurs.
What it looks likeA user-facing problem is handled only through an assertion.
Why it failsThe source recommends using assert judiciously and generally favors catching exceptions, handling the problem or displaying a message, and then quitting.
Do this insteadConsider exception handling for problems that need an explicit response.
Message Design Practice
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.