"I want to show you an error, dear user, but it is a bit lengthy..."

My question is not only Python-specific. I’ve meet with my problem when I was making a compiler compiler (first written in Python, now during porting to C++). Such a program requires a very specific knowledge to use it properly, however I wanted it to be used by a broad audience.

And thus the problem: usually when something bad happens a program should show some kind of a short message to the user. However in the case of that program I wanted to give also some clue as to how fix it, e.g. telling the user how such an error appeared and giving hints of how to get rid of it. I’m afraid though that I may making a thing like the infamous C++ compilers’ “litany of error dumps” (where a simple lack of ; may give you 4 screens of text).

So, how long an error message can be?

1 Like

Even asking this question is putting more effort into it than most developers, IMX :slight_smile:

There are three main reasons why C++ compilers show you so much error text: because there can be multiple things wrong at once; because, after the first thing goes wrong, they have to guess what you meant, which means possibly misinterpreting the rest in context; and because of fully expanding templated typenames. None of these really apply to a runtime error in Python - instead, the error is long by default because it includes a complete stack trace. If your code has an explicit raise to give a custom message - and you know what’s wrong at that point, and don’t expect any further stack trace “behind” that to be useful - you can raise your exception from None to suppress exception chaining.

If you end up needing a detailed error dump, one useful idea is to wrap your main loop in an exception handler that writes out the full stack trace to a file instead of stderr. That gives you the opportunity to add additional framing details (e.g. print a friendlier message that simply informs the user that something went wrong, and says where to find the error-dump file), choose a useful name for the file (maybe it should include a timestamp?), etc.

Any more specific advice than that will depend on the details of how your program works, and will require serious critical thinking and design work, mainly on your part. It’s simply not something that can just be answered on a forum.


Modern compilers are supposed to deduce that the “;” is missing and limit the error message cascade. That problem should be a thing of the past.

Short is not a goal, informative should be the goal.
When I make an mistake I want to be guided to the solution.

I like the rust compilers error messages. Especially the suggested fix feature
(that an IDE can automat applying).

If you have not played with rust you will find its approach to errors interested.

1 Like

Do they? Can’t they just tell me that first thing and then shut up?

If it’s in an IDE, then stopping at the first error and positioning the cursor there is good enough, provided that the compilation is quick.

I prefer to get the first few errors rather than one at a time.
Of course if there are lots then I can just view and fix the first and click the build button, but if only one error is reported I cannot see the wider context of my mistakes or misunderstandings.

1 Like

I don’t think the issue is about error message length, but error message content. That has always been a weakness of programming languages.

Really the first thing a preprocessor should do is look for common errors like a missing semi-colon at the end of a line, unclosed quotes, “=” (assignment) vs “==” (comparison), etc. Then display a suggestion like “Perhaps you are missing a colon in this line (display the line number)”.

I’m finding the stack traces from Python errors a bit lengthy, as most of the time the last 2 lines of the error message is what I need to find the problem. But I also recognize that a stack trace is needed in some issues too.

On a side note there is an AI that will look at your code and make suggestions, but it might not be free. You do have to sign up for it. I don’t recall the name.

1 Like

Well, I suppose they don’t have to. They could abort the compilation process instead. I’m not aware of any C compilers that actually do this, although I think I’ve seen other language compilers work that way.

But apparently a lot of users really want to know “everything that’s wrong” when running the compiler, and don’t want to be compelled to fix the first error first in order to proceed.

Well, how I remember it is that most of the time there isn’t anything else wrong. Like, you forget a single semicolon and get five pages of a cryptic mess, you scroll up to find the first message, add the semicolon, and poof - “all the errors” are gone.

Omitting a closing brace has the same effect.

#define true ((rand() % 2) == 0)

Might compile but mess with… stuff.

Or if ya really feel it, odd a curly brace to ^