Warning: mysqli::__construct(): (HY000/1203): User howardkn already has more than 'max_user_connections' active connections in D:\Inetpub\vhosts\howardknight.net\al.howardknight.net\includes\artfuncs.php on line 21
Failed to connect to MySQL: (1203) User howardkn already has more than 'max_user_connections' active connections
Warning: mysqli::query(): Couldn't fetch mysqli in D:\Inetpub\vhosts\howardknight.net\al.howardknight.net\index.php on line 66
Article <mailman.70.1712250047.3468.python-list@python.org>
Deutsch   English   Français   Italiano  
<mailman.70.1712250047.3468.python-list@python.org>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: Klaas van Schelven <klaas@vanschelven.com>
Newsgroups: comp.lang.python
Subject: How to get insight in the relations between tracebacks of exceptions
 in an exception-chain
Date: Thu, 4 Apr 2024 09:04:50 +0200
Lines: 99
Message-ID: <mailman.70.1712250047.3468.python-list@python.org>
References: <CACu42+76QaxqE9DNDZxM=LJGT0r287Vh8p42E5=bUq3+K9LHdw@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de 39ZK+gLMZU5XUT4R0qDpKgLGe6pwjE8lWAb2azNia0rQ==
Cancel-Lock: sha1:U5ewvgQgP3pIf8sOn/bVAxLmAQw= sha256:ooXRICzmUKdQwJ7qqZrIK5Lu09rvEAETnZb28nU6Axg=
Return-Path: <klaasvanschelven@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
 dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.042
X-Spam-Evidence: '*H*': 0.92; '*S*': 0.00; '17,': 0.04; 'def': 0.04;
 'traceback': 0.04; '(most': 0.05; 'last):': 0.05; 'creators':
 0.07; 'programmer': 0.07; 'hard.': 0.09; 'skip:_ 20': 0.09;
 'skip:` 10': 0.09; 'subject:between': 0.09; 'trivial': 0.09;
 '(potentially': 0.16; '_not_': 0.16; 'annotated': 0.16; 'bottom-
 up': 0.16; 'frame,': 0.16; 'frames': 0.16; 'outer': 0.16;
 'presumably': 0.16; 'program:': 0.16; 'somewhat': 0.16; 'source-
 code': 0.16; 'view.': 0.16; 'problem': 0.16; 'python': 0.16;
 'it?': 0.19; 'to:addr:python-list': 0.20; 'exception': 0.22;
 "what's": 0.22; 'subject:How': 0.23; '(and': 0.25; 'seems': 0.26;
 'task': 0.26; 'leave': 0.27; 'asked': 0.29; 'present': 0.30;
 'raise': 0.31; 'question': 0.32; 'answers': 0.32; 'happening':
 0.32; 'message-id:@mail.gmail.com': 0.32; 'but': 0.32; "i'm":
 0.33; 'path': 0.33; 'same': 0.34; 'received:google.com': 0.34;
 'running': 0.34; '"the': 0.35; 'fine': 0.35; 'handling': 0.35;
 'following': 0.35; 'cases': 0.36; 'people': 0.36; 'special': 0.37;
 'received:209.85': 0.37; 'class': 0.37; 'file': 0.38; 'way': 0.38;
 'read': 0.38; 'received:209': 0.39; 'received:209.85.208': 0.39;
 'use': 0.39; 'program.': 0.40; 'wants': 0.40; 'want': 0.40;
 'best': 0.61; '10,': 0.61; 'skip:o 10': 0.61; 'above': 0.62;
 'arrive': 0.62; 'here': 0.62; 'skip:o 20': 0.63; 'copy': 0.63;
 'between': 0.63; 'leading': 0.63; 'pass': 0.64; 'complete': 0.64;
 'skip:r 20': 0.64; 'about.': 0.64; 'thus': 0.64; 'less': 0.65;
 '20,': 0.67; 'url-ip:104.18/16': 0.67; 'per': 0.68; 'during':
 0.69; 'order': 0.69; 'latter': 0.69; 'perfectly': 0.69;
 'presenting': 0.69; 'raised': 0.70; 'subject:get': 0.70;
 'interesting': 0.71; 'tools': 0.74; 'analyze': 0.75; 'loss': 0.75;
 'led': 0.81; 'moment': 0.81; 'handled.': 0.84; 'mentally': 0.84;
 'skip:" 40': 0.84; 'skip:& 50': 0.84; 'subject: \n ': 0.84;
 'laid': 0.91
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1712214302; x=1712819102;
 h=to:subject:message-id:date:from:mime-version:x-gm-message-state
 :from:to:cc:subject:date:message-id:reply-to;
 bh=bXne7EgyX+n6RMk+O6fVPyF08MoiA+k5sjuqfEwDh+c=;
 b=K4hotPjx/g61+zUI63KHWewueuKzwqT4tkvsryV4YF1D9ebHMv/B4yYNBJQBHMke/8
 v6P82ZMptViznNEk+w53xSCw6AUPld55vkivUs76SaczDtUVIUTdFfPGL4PpBCMOJVVL
 ON57tzl8zGf5ZzntgpPYyE+AQmaL8f4eClKaUR5RfavmYr4imZQVL9uQpztCl2s1OIGo
 TNZVSSJTo7OJZFkxvdWsALlwdYn3a0C9FAvsKVTob9c1ino5GpK4CNkb8zYoQnBwXy8D
 z3mbYJ9Uj6EGhfLQIKyD8jL7MpeKrceUARDQcaANc2z99MEOJxeUEOivoRKxo0Vk5f3o
 YVbg==
X-Gm-Message-State: AOJu0Yz0OMDc/d6BMJu7UfYFz0wtTbUjjJeZRqtTOhlY1Sd1l2GZmwdS
 u7uJqthOgwB5Fqq73lrG1swno20alYHXSgYYjiRaAagksT+t4kMM3C8QUMFK79b8LHbDbHTIfkB
 3ntzQPHT38pbZKv02zvg3kyXlpwASwROsVXs=
X-Google-Smtp-Source: AGHT+IHcwOHB1ycvtGmdJkV40B5AkBttfb9scot3O9cEnGlkRLc0VF3//Kbqr2h6y3DYTOoyeb+Wp8fY0+SWuCqNO0g=
X-Received: by 2002:a50:d5d6:0:b0:56d:fb36:c388 with SMTP id
 g22-20020a50d5d6000000b0056dfb36c388mr1535789edj.9.1712214301795; Thu, 04 Apr
 2024 00:05:01 -0700 (PDT)
X-Mailman-Approved-At: Thu, 04 Apr 2024 13:00:46 -0400
X-Content-Filtered-By: Mailman/MimeDel 2.1.39
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
 <python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
 <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
 <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <CACu42+76QaxqE9DNDZxM=LJGT0r287Vh8p42E5=bUq3+K9LHdw@mail.gmail.com>
Bytes: 8420

Hi,

This question is best introduced example-first:

Consider the following trivial program:

```
class OriginalException(Exception):
    pass


class AnotherException(Exception):
    pass


def raise_another_exception():
    raise AnotherException()


def show_something():
    try:
        raise OriginalException()
    except OriginalException:
        raise_another_exception()


show_something()
```

running this will dump the following on screen (minus annotations on the
Right-Hand-Side):

```
Traceback (most recent call last):
  File "...../stackoverflow_single_complication.py", line 15, in
show_something                 t1
    raise OriginalException()
__main__.OriginalException

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "...../stackoverflow_single_complication.py", line 20, in <module>
                    t0
    show_something()
  File "...../stackoverflow_single_complication.py", line 17, in
show_something                 t2
    raise_another_exception()
  File "...../stackoverflow_single_complication.py", line 10, in
raise_another_exception        t3
    raise AnotherException()
__main__.AnotherException
```

What we see here is first the `OriginalException` with the stackframes
between the moment that it was raised and the moment it was handled.
Then we see `AnotherException`, with _all_ a complete traceback from its
point-of-raising to the start of the program.

In itself this is perfectly fine, but a consequence of this way of
presenting the information is that the stackframes are _not_ laid out on
the screen in the order in which they were called (and not in the reverse
order either), as per the annotations _t1_, _t0_, _t2_, _t3_. The path
leading up to _t1_ is of course the same as the path leading up to _t2_,
and the creators of Python have chosen to present it only once, in the
latter case, presumably because that Exception is usually the most
interesting one, and because it allows one to read the bottom exception
bottom-up without loss of information. However, it does leave people that
want to analyze the `OriginalException` somewhat mystified: what led up to
it?

A programmer that wants to understand what led up to _t1_ would need to
[mentally] copy all the frames above the point _t2_ to the first stacktrace
to get a complete view. However, in reality the point _t2_ is, AFAIK, not
automatically annotated for you as a special frame, which makes the task of
mentally copying the stacktrace much harder.

Since the point _t2_ is in general "the failing line in the `except`
block", by cross-referencing the source-code this excercise can usually be
completed, but this seems unnecessarily hard.

**Is it possible to automatically pinpoint _t2_ as the "handling frame"?**

(The example above is given without some outer exception-handling context;
I'm perfectly fine with answers that introduce it and then use `traceback`
or other tools to arrive at the correct answer).

This is the most trivial case that illustrates the problem; real cases have
many more stack frames and thus less clearly illustrate the problem but
more clearly illustrate the need for (potentially automated) clarification
of what's happening that this SO question is about.


regards,
Klaas


Previously asked here:
https://stackoverflow.com/questions/78270044/how-to-get-insight-in-the-relations-between-tracebacks-of-exceptions-in-an-excep