Skip to content

Managed exception from Python code is always transformed to PythonException #1443

@tibortakacs

Description

@tibortakacs

Environment

  • Pythonnet version: 3.0.0 preview
  • Python version: 3.8 win32
  • Operating System: Windows
  • .NET Runtime: .NET 5

Summary

If there is a callback from .NET to Python, and the Python code raises a .NET type exception, instead of the corresponding real .NET exception was rethrown on the .NET side (after marshalling), always Python.Runtime.PythonException is thrown.

Details

.NET snippet

class Foo
{
    public static void Bar(Action action)
    {
        try
        {
            action.Invoke();
            Console.WriteLine("No exception.");
        }
        catch (NullReferenceException)
        {
            Console.WriteLine("Null reference caught.");
        }
        catch (Exception ex)
        {
             Console.WriteLine("General exception caught: " + ex.GetType().Type); 
        }
    }
}

Python snippet

# .NET imports
from System import NullReferenceException, Action
from MyProgram import Foo

# Preparation
def call():
    raise NullReferenceException()

action = Action(call)

# Action
Foo.Bar(action)

Expected output:
Null reference caught.

Actual output:
General exception caught: Python.Runtime.PythonException

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions