Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[FR] add exception handling after JNI calls

See original GitHub issue

Currently all JNI calls made unchecked. The checked call demo:

function test1(name)
  return android.jni:context(, function(jni)
    local clz, e = jni.env[0].FindClass(jni.env, name), jni.env[0].ExceptionCheck(jni.env)
    if e == 0 then
      e = nil
      local ex = jni.env[0].ExceptionOccurred(jni.env)
      e = jni:to_string(
        jni:callObjectMethod(ex, "getMessage", "()Ljava/lang/String;")
    return clz, e
local clzz, e = test1("java/lang/WRONGNAME")
if e then

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

pazoscommented, Dec 20, 2020

jni.env[0].FindClass(jni.env, name) is a cool stuff if you’re implementing method invokation via reflection, because the name of the class in that case is not part of the API, and you’ll need to know if it is available on a given device. In that case makes sense to check exceptions before calling the method.

pazoscommented, Dec 20, 2020

As the result I just annoy you with non-KR related ideas. Right? Then it’s time for me just to move away.

Not interested doesn’t mean annoyed. PRs are welcome for stuff that doesn’t relate to KOReader. But in this case is not a PR, but a feature request which doesn’t serve any purpose.

To call java from lua you’ll need to know:

  1. the name of the class
  2. the name of the method
  3. the type of the method
  4. method’s signature

Any typo or mismatch in any element would make your code crash. And because lua is dynamically typed luacheck won’t alert you as both “()Ljava/lang/String;” and “()Ljava/io/File;” are valid lua strings, but one is a valid signature for the method "getMessage" and the other is not.

The same happens when you’re checking exceptions: you call jni:callObjectMethod. If you call jni:callStaticObjectMethod you’ll trigger an error, because there isn’t any static method which returns an object named “getMessage” for the class ex.

Catching JNI exceptions isn’t a bad thing to add, but to serve a purpose it needs to thow the exception and interrupt the app flow, so the result would be exactly the same: a crash, but the way to retrieve the bug would be better as it would be associated to the app name and not a RuntimeError (mismatch signature) or a native crash (LuaJIT error dealing with libraries using FFI)

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to catch JNI/Java Exception - Stack Overflow
if you invoke a Java method from JNI, calling ExceptionCheck afterwards will return JNI_TRUE if an exception was thrown by the Java.
Read more >
Exception Handling in JNI |
In the native code, what we can do is call either the ExceptionOccurred() or ExceptionCheck() JNI function just after the native function call....
Read more >
Exception handling in JNI and throwing it in Java
Here I explain exception handling in JNI (Java Native Interface). Check for ... This is done through the following JNI function call: void ......
Read more >
JNI: Check & Handle Exception after calling back into Java
If you do not check, the next time you go through the JNI, the JNI code detects a pending exception and throws it....
Read more >
[Java API] Validate that there is a check for an exception after ...
"For new JNI code validate that there is a check for an exception after all JNI calls that can raise an exception." Current...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found