`else` block in `try` statement swallows exceptions

Open
#615 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
javascript, python
Domain
compilers

Research direction

No source file or test is named. Start by tracing how the compiler translates the provided Python try/except/else example, then verify that an exception raised in the else block reaches the outer context instead of being swallowed; issue #99 may provide related history.

Written by the indexing model from the issue text.

Description

STATE: under consideration

Exceptions raised in the else block should bubble out, but instead they currently get suppressed.

This:

try:
    pass
except:
    pass
else:
    raise Exception('something wrong happened in the `else`')

compiles to:

try {
        // pass;
        try {
                var __except0__ = Exception ('something wrong happened in the `else`');
                __except0__.__cause__ = null;
                throw __except0__;
        }
        catch (__except0__) {
        }
}
catch (__except0__) {
        // pass;
}

I suggest: set a temporary boolean variable to true before the try, then flip it to false as the very first statement inside the catch, and then have an if statement on that boolean as the very first statement after the catch block (it might be better to just always generate that if inside a finally in the JavaScript even if there is no finally in the Python, because it has to execute before any finally if it does exist).

(My reasoning for flipping the boolean as the first statement in the catch instead of the last statement in the try is that if for some reason ever a JavaScript implementation existed that raised an exception while trying to set that internal implementation book-keeping variable, I think it better that our generated code explode that up a level, instead of it being caught by the local except block, because logically that is an error within the try statement itself, not the block of code inside it.)

P.S. In similar situations in the future, would you prefer I reopened #99 instead of creating this new issue?

Dominant language
Python
Stars
2.9k
Forks
218
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from TranscryptOrg/Transcrypt

All issues in TranscryptOrg/Transcrypt

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.