Skip to content

Add access to exception instance on stack - #134883

Open
jkotas with Copilot wants to merge 9 commits into
mainfrom
copilot/api-proposal-access-exception-instance
Open

jkotas with Copilot wants to merge 9 commits into
mainfrom
copilot/api-proposal-access-exception-instance

Conversation

Copilot AI commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #89724

@azure-pipelines

azure-pipelines Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
@jkotas

jkotas commented Sep 29, 2026

Copy link
Copy Markdown
Member

@copilot Can you check the behavior for exceptions that do not inherit from Exception?

…etCurrentException

Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>

Copilot AI commented Sep 29, 2026

Copy link
Copy Markdown
Contributor Author

@copilot Can you check the behavior for exceptions that do not inherit from Exception?

I checked. CoreCLR and NativeAOT behaved differently, so I changed NativeAOT to match CoreCLR (0632034):

  • CoreCLR: The runtime already wraps a non-Exception object in a RuntimeWrappedException when it is thrown (NormalizeThrownObject). So GetCurrentException() returned that wrapper, the same instance the catch handler sees. WrappedException holds the original object.
  • NativeAOT: The runtime never wraps these objects (known issue dotnet/runtimelab#155), so the raw object stays in the ExInfo. Our as Exception turned it into null, which would report "nothing in flight" while something was. Now GetCurrentException wraps the object in a new RuntimeWrappedException. The catch type is the same as CoreCLR, but you get a new wrapper instance on each call.

I also documented this in the API remarks and added a test that throws a string from a DynamicMethod. It passes on CoreCLR (all 12 tests in the class ran). The test needs Reflection.Emit, so it doesn't run on NativeAOT. The NativeAOT change compiles, but I have not run it.

Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
@jkotas

jkotas commented Oct 10, 2026

Copy link
Copy Markdown
Member

@copilot Change the design to return exception being caught inside the catch block. CatchHandlerRunningFlag should be unncessary with this change.

Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>

Copilot AI commented Oct 10, 2026

Copy link
Copy Markdown
Contributor Author

@copilot Change the design to return exception being caught inside the catch block. CatchHandlerRunningFlag should be unncessary with this change.

Updated the behavior so GetCurrentException() returns the exception being caught and removed CatchHandlerRunningFlag from CoreCLR and NativeAOT. Added catch-handler assertions and updated the API remarks. Commit: 765d3a8a.

Copilot AI requested a review from jkotas October 10, 2026 19:51
Comment thread src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/ExceptionHandling.cs Outdated
Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Copilot AI requested a review from jkotas October 10, 2026 22:37
@jkotas
jkotas requested a balanced review from Copilot October 10, 2026 22:49
@jkotas jkotas removed the blocked Issue/PR is blocked on something - see comments label Oct 10, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The public documentation omits the PlatformNotSupportedException that Mono always throws.

1 open finding
1 resolved since last review

🧠 Review effort: Balanced

Comment thread src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/ExceptionHandling.cs Outdated
Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Copilot AI requested a review from jkotas October 13, 2026 00:28
@jkotas
jkotas requested a review from janvorli October 13, 2026 01:34

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-ExceptionHandling-coreclr only use for closed issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[API Proposal]: Let accessing the instance of Exception (if any) on stack.

5 participants