+
Skip to content

Add an event for unhandled exceptions from delegates invoked by native code #860

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/Libs/GLib-2.0/Public/UnhandledException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;

namespace GLib;

/// <summary>
/// Allows handlings exceptions which can't cross the native code boundary and
/// would terminate the application.
/// </summary>
public static class UnhandledException
{
/// <summary>
/// Invoked if an exception is raised which reaches the native code boundary.
/// This can be used by applications to handle these exceptions gracefully
/// without terminating the application.
/// </summary>
public static event EventHandler<UnhandledExceptionEventArgs>? Raised;

/// <summary>
/// Call this method to invoke the "Raised" event. If the event has no subscribers,
/// the application is terminated.
/// </summary>
/// <remarks>
/// This method is not intended to be called by application code, and should only
/// be called by code which catches an exception that would otherwise terminate
/// the application by unwinding to the native code boundary.
/// </remarks>
public static void Raise(Exception e)
{
if (Raised is null)
{
Console.Error.WriteLine($"{nameof(GLib.UnhandledException)} - unhandled exception: {e}");
Environment.Exit(1);
}
else
{
Raised.Invoke(null, new UnhandledExceptionEventArgs(e, false));
}
}
}
9 changes: 8 additions & 1 deletion src/Libs/GObject-2.0/Public/Closure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@ private void InternalCallback(IntPtr closure, IntPtr returnValuePtr, uint nParam
.Select(valueHandle => new Value(valueHandle))
.ToArray();

_callback(returnValue, paramValues);
try
{
_callback(returnValue, paramValues);
}
catch (Exception e)
{
GLib.UnhandledException.Raise(e);
}
}

public void Dispose()
Expand Down
28 changes: 28 additions & 0 deletions src/Tests/Libs/GirTest-0.1.Tests/UnhandledExceptionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GirTest.Tests;

[TestClass, TestCategory("BindingTest")]
public class UnhandledExceptionTest : Test
{
[TestMethod]
public void SupportsSignalHandlers()
{
var tester = ReturningSignalTester.New();
tester.OnReturnBool += (_, _) =>
{
throw new NotImplementedException();
};

bool exceptionCaught = false;
GLib.UnhandledException.Raised += (_, args) =>
{
exceptionCaught = args.ExceptionObject is NotImplementedException;
};

tester.EmitReturnBool().Should().Be(false);
exceptionCaught.Should().Be(true);
}
}
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载