HaxeFoundation / HaxeFoundation/haxe

[python] Throwing an instance of (or from) an `Exception` class throws a TypeError.

Open
#9,128 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Haxe
Stars
6.9k
Forks
715
Avg merge
2d 2h
Merged PRs (30d)
11

Description

Throwing an instance of a class `Exception`, or throwing an exception from a class `Exception`, throws a TypeError exception. Catching a thrown instance of a class `Exception`, or catching an exception thrown from a class `Exception`, throws another TypeError exception.

```haxe
class Main {
static function main() {
try {
throw new Exception();
} catch (ex:Dynamic) {}
}
}

class Exception {
public function new() {}
}
```

When compiling the above in `Main.hx` with `haxe --main Main --python run.py` and running with `python3 run.py`, this happens:

```
Traceback (most recent call last):
File "run.py", line 13, in main
raise _HxException(Exception())
File "run.py", line 65, in __init__
super().__init__(message)
TypeError: __init__() takes 1 positional argument but 2 were given

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "run.py", line 83, in
Main.main()
File "run.py", line 14, in main
except Exception as _hx_e:
TypeError: catching classes that do not inherit from BaseException is not allowed
```

Tested with "haxe_2020-02-05_development_e686af4" using Python 3.7.5.

Here's the compiled `run.py` file, but with a txt extension so that GitHub will accept it: [run.txt](https://github.com/HaxeFoundation/haxe/files/4173803/run.txt).

The same issue occurs if throwing an exception from the `Exception` class like so:
```haxe
class Main {
static function main() {
try {
new Exception();
} catch (ex:Dynamic) {}
}
}

class Exception {
public function new() {
throw("Hello, World!");
}
}
```

If I had to guess I'd say that Python doesn't like a class `"Exception"` existing, because this doesn't happen if I rename `Exception` to `MyException`.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.