Skip to content

Conversation

psiddh
Copy link
Contributor

@psiddh psiddh commented Aug 19, 2025

Add ExecutorchRuntimeException

Test Plan: Tested with executorch-examples and llama android demo.

Reviewers:

Subscribers:

Tasks:

Tags:

Summary

[PLEASE REMOVE] See CONTRIBUTING.md's Pull Requests for ExecuTorch PR guidelines.

[PLEASE REMOVE] If this PR closes an issue, please add a Fixes #<issue-id> line.

[PLEASE REMOVE] If this PR introduces a fix or feature that should be the upcoming release notes, please add a "Release notes: " label. For a list of available release notes labels, check out CONTRIBUTING.md's Pull Requests.

Test plan

[PLEASE REMOVE] How did you test this PR? Please write down any manual commands you used and note down tests that you have written if applicable.

Copy link

pytorch-bot bot commented Aug 19, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/13526

Note: Links to docs will display an error until the docs builds have been completed.

❌ 3 New Failures

As of commit e9cf866 with merge base 6e520e2 (image):

NEW FAILURES - The following jobs have failed:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 19, 2025
Copy link

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@psiddh psiddh force-pushed the android_exceptions branch 3 times, most recently from 4f0f4d2 to bc8f701 Compare August 20, 2025 16:32
@psiddh psiddh changed the title Summary: Add initial basic infra to throw relevant exceptions from Summary: Add ExecutorchRuntimeException: Used to throw relevant exceptions from JNI layer in the event of errors Aug 20, 2025
@psiddh psiddh changed the title Summary: Add ExecutorchRuntimeException: Used to throw relevant exceptions from JNI layer in the event of errors Summary: Add ExecutorchRuntimeException: Throw relevant exceptions from JNI layer in the event of errors Aug 20, 2025
non_fbcode_target(_kind = fb_android_cxx_library,
name = "executorch_jni",
srcs = ["jni_layer.cpp", "log.cpp", "jni_layer_runtime.cpp"],
srcs = ["jni_layer.cpp", "log.cpp", "jni_layer_runtime.cpp", "jni_helper.cpp"],
Copy link
Contributor

Choose a reason for hiding this comment

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

can you refactor these srcs into a list that can be shared across the libs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@psiddh psiddh force-pushed the android_exceptions branch 2 times, most recently from f48dd0c to 5409ecd Compare August 21, 2025 03:57
Android Jni layer

Test Plan: Tested executorch.jar with executorch demo example and llama demo app

Reviewers:

Subscribers:

Tasks:

Tags:
Copy link
Member

@GregoryComer GregoryComer left a comment

Choose a reason for hiding this comment

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

Overall, looks great to me. Thanks for taking this. The only thing I wanted to get your thoughts on is whether we should update the Java definition in Module.java to indicate that it throws. I'm not as familiar with some of the Java conventions, but I believe we can add a checked exception specification to the Java declarations of the JNI methods to indicate that they can throw an ExecutorchRuntimeException.

From what I've seen, opinion seems to be divided on checked exceptions. I don't have a strong opinion either way, and it would potentially require some synchronous code changes to land internally. The advantage might be that it's more clear in the signature that various methods (Module.load / execute) can throw an ExecutorchRuntimeException.


jni_helper::throwExecutorchException(
static_cast<uint32_t>(
Error::InvalidArgument), // For backward compatibility
Copy link
Member

Choose a reason for hiding this comment

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

From looking at user code, I'm a bit less worried about about strict BC here, so I think you could use the actual error code and not worry about matching the existing exception type.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree, it is always better to throw the exception with native error code as-is. I wasn't sure 100% abt it, but since you checked the client code, I will make the change

@GregoryComer
Copy link
Member

You're probably already be planning this, but could you add/modify an integration test to cover thrown exceptions?

Copy link
Member

@GregoryComer GregoryComer left a comment

Choose a reason for hiding this comment

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

Talked a bit with Sid offline. Sounds like unchecked exceptions are the better option here, which is good with me.

@psiddh
Copy link
Contributor Author

psiddh commented Aug 22, 2025

From

If the native side (JNI C++ code) throws a RuntimeException or IllegalArgumentException (which is what we are doing now), these are unchecked exceptions in Java afaik.

  • Unchecked exceptions do not need to be declared in the Java method signature with throws.
  • It is considered good practice to omit throws for unchecked exceptions because:
  • These exceptions represent programming errors or unexpected conditions.Declaring them clutters the API without adding compile-time safety.

@psiddh
Copy link
Contributor Author

psiddh commented Aug 22, 2025

You're probably already be planning this, but could you add/modify an integration test to cover thrown exceptions?

Yes was planning on a follow up change

@psiddh psiddh merged commit 8aca197 into main Aug 22, 2025
101 of 104 checks passed
@psiddh psiddh deleted the android_exceptions branch August 22, 2025 06:18
agrima1304 pushed a commit to agrima1304/executorch that referenced this pull request Aug 26, 2025
…om JNI layer in the event of errors (pytorch#13526)

Add ExecutorchRuntimeException

Test Plan: Tested with executorch-examples and llama android demo.

Reviewers:

Subscribers:

Tasks:

Tags:

### Summary
[PLEASE REMOVE] See [CONTRIBUTING.md's Pull
Requests](https://github.com/pytorch/executorch/blob/main/CONTRIBUTING.md#pull-requests)
for ExecuTorch PR guidelines.

[PLEASE REMOVE] If this PR closes an issue, please add a `Fixes
#<issue-id>` line.

[PLEASE REMOVE] If this PR introduces a fix or feature that should be
the upcoming release notes, please add a "Release notes: <area>" label.
For a list of available release notes labels, check out
[CONTRIBUTING.md's Pull
Requests](https://github.com/pytorch/executorch/blob/main/CONTRIBUTING.md#pull-requests).

### Test plan
[PLEASE REMOVE] How did you test this PR? Please write down any manual
commands you used and note down tests that you have written if
applicable.

Co-authored-by: Github Executorch <github_executorch@arm.com>
jackzhxng added a commit that referenced this pull request Aug 26, 2025
…tions from JNI layer in the event of errors (#13526)"

This reverts commit 8aca197.
jackzhxng added a commit that referenced this pull request Aug 26, 2025
Revert #13526 and #13640 to land as one internally
psiddh added a commit to psiddh/executorch that referenced this pull request Aug 26, 2025
…om JNI layer in the event of errors (pytorch#13526)

Summary: Add ExecutorchRuntimeException

Reviewed By: jackzhxng

Differential Revision: D81076817
facebook-github-bot pushed a commit that referenced this pull request Aug 27, 2025
…om JNI layer in the event of errors (#13526)

Differential Revision: D81076817

Pull Request resolved: #13714
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants