Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

JNA should include std::exception::what() in thrown `java.lang.Error`

未关闭
#1,192 5 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
25/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
cpp, java
领域
backend, tooling

调研方向

从 Native.loadLibrary 绑定路径开始,跟踪原生 std::exception 如何变成 java.lang.Error。将该路径与 issue 中的 InvocationMapper 和 GetLastError workaround 进行比较。完成标准是,抛出的错误包含 std::exception::what(),而不是只报告无效内存访问,并且已根据 Windows 示例检查其行为。

由索引模型根据 Issue 内容生成。

描述

feature request

As a caller of a cpp hunk of math I want std::exceptions to be nicely translated by JNA across the bindings so that I can more easily debug runtime problems.

I have some cpp code that I've updated to throw std::exception's that contain valuable data in their what() clause. What I was hoping JNA would do is this:

//mylib.dll
#include <exception>

extern "C" __declspec(dllexport) 
const void MYCOMPANY_compute_eigen_values_or_something()
{
  throw std::exception("message from native code: kablamo!")
}
interface MyLib: Library {
  public void MYCOMPANY_compute_eigen_values_or_something()
}
var lib = Native.loadLibrary("mylib", MyLib.class, options)
try { lib.MYCOMPANY_compute_eigen_values_or_something() }
catch(Throwable ex){
  System.out.println(ex)
}

expected: java.lang.Error: In native code: message from native code: kablamo!
actual: java.lang.Error: Invalid memory access

... on: Windows 10 1903, JNA 4.2.2


My work around:
Wrap my code in a try { } catch { }, save it to a global variable, and expose a function GetLastError() that I then call from the handler. If you combine this with StackWalker you can even get a java--ish native code stack trace in your preconditions/postconditions. From the java side, I add my own InvocationMapper to catch JNA thrown java.lang.Errors, call GetLastError() then throw a new exception with the results.

主要语言
Java
星标
8.9k
派生
1.7k
PR 合并指标
30 天内没有已合并 PR

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

java-native-access/jna 的其他 Issue

查看 java-native-access/jna 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。