IronLanguages / ironpython3

Implementation of Python 3.x for .NET Framework that is built on top of the Dynamic Language Runtime.
Apache License 2.0
2.51k stars 290 forks source link

Feature: Unify `System.Exception` with `BaseException` #1816

Open Lamparter opened 1 week ago

Lamparter commented 1 week ago

[!NOTE] This issue is the perfect opportunity to demonstrate the new issue templates introduced in #1812!

📄 Description

Currently, raising a .NET exception that derives from System.Exception using the raise keyword results in the following error: TypeError: exceptions must derive from BaseException. The IronPython compiler should allow exceptions from .NET classes to be raised using Python's built in raise function.

🗃️ Alternative solutions

👥 Intended Use-Case

This would allow for better use of the existing CLR exception types.

📸 Assets

image

slozier commented 5 days ago

You can raise an instance of a .NET exception. For example:

>>> raise System.Runtime.InteropServices.COMException()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
SystemError: Error HRESULT E_FAIL has been returned from a call to a COM component.

I guess we could also instantiate the exception when trying to raise a class derived from System.Exception.