[GH-ISSUE #104] How do we handle .NET exceptions? #60

Open
opened 2026-05-05 11:02:55 -06:00 by gitea-mirror · 2 comments
Owner

Originally created by @pauldotknopf on GitHub (Feb 6, 2019).
Original GitHub issue: https://github.com/qmlnet/qmlnet/issues/104

This issue serves as a discussion point for handling .NET exceptions in QML.

Originally created by @pauldotknopf on GitHub (Feb 6, 2019). Original GitHub issue: https://github.com/qmlnet/qmlnet/issues/104 This issue serves as a discussion point for handling .NET exceptions in QML.
Author
Owner

@pauldotknopf commented on GitHub (Feb 6, 2019):

I'd like to see .NET exceptions raised in QML like regular JS exceptions.

Starting in Qt 5.12, there is a QJSEngine->throwError method (see here) for raising JS exceptions from c++. This would allow something like this.

public class NetObject
{
    public void Method()
    {
        throw new Exception("exception from .NET")
    }
}
Item {
    NetObject {
        id: netObject
    }

    Component.onCompleted: {
        try {
            netObject.method()
        } catch (err) {
            console.log(err) // outputs: exception from .NET
        }
    }
}
<!-- gh-comment-id:460945024 --> @pauldotknopf commented on GitHub (Feb 6, 2019): I'd like to see .NET exceptions raised in QML like regular JS exceptions. Starting in Qt 5.12, there is a ```QJSEngine->throwError``` method (see [here](http://doc.qt.io/qt-5/qjsengine.html#throwError)) for raising JS exceptions from c++. This would allow something like this. ```c# public class NetObject { public void Method() { throw new Exception("exception from .NET") } } ``` ```qml Item { NetObject { id: netObject } Component.onCompleted: { try { netObject.method() } catch (err) { console.log(err) // outputs: exception from .NET } } } ```
Author
Owner

@pauldotknopf commented on GitHub (Feb 6, 2019):

In order to propogate errors up through all levels, all callbacks/delegates need to return a new int type.

#define NET_RESULT_OK 0
#define NET_RESULT_FAILURE 1 // generic exception, no known cause
#define NET_RESULT_EXCEPTION 2 // an exception, restore the .NET exception and re-throw.

If we ever invoke a native method from .NET that returns this exception code, we need to check for any pending errors.

Since .NET exceptions can't be marshalled, we must store them in some ambient location. This is the perfect use-case for QThreadStorage (see here).

<!-- gh-comment-id:460945827 --> @pauldotknopf commented on GitHub (Feb 6, 2019): In order to propogate errors up through all levels, all callbacks/delegates need to return a new ```int``` type. ```c++ #define NET_RESULT_OK 0 #define NET_RESULT_FAILURE 1 // generic exception, no known cause #define NET_RESULT_EXCEPTION 2 // an exception, restore the .NET exception and re-throw. ``` If we ever invoke a native method from .NET that returns this exception code, we need to check for any pending errors. Since .NET exceptions can't be marshalled, we must store them in some ambient location. This is the perfect use-case for ```QThreadStorage``` (see [here](http://doc.qt.io/qt-5/qthreadstorage.html)).
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github-starred/qmlnet#60
No description provided.