{"record":{"id":"7f9f200da39d0b74","repo":"java-native-access/jna","slug":"jna-error-while-handling-callback-exception-continuing","errorCode":null,"errorMessage":"JNA: error while handling callback exception, continuing\n","messagePattern":"JNA: error while handling callback exception, continuing\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"native/callback.c","lineNumber":557,"sourceCode":"      }\n    }\n  }\n  else {\n    jobject result;\n    jobjectArray params =\n      (*env)->NewObjectArray(env, cif->nargs, classObject, NULL);\n    unsigned int i;\n\n    for (i=0;i < cif->nargs;i++) {\n      jobject arg = new_object(env, cb->arg_jtypes[i], cbargs[i], JNI_FALSE, cb->encoding);\n      (*env)->SetObjectArrayElement(env, params, i, arg);\n    }\n    result = (*env)->CallObjectMethod(env, self, cb->methodID, params);\n    if ((*env)->ExceptionCheck(env)) {\n      jthrowable throwable = (*env)->ExceptionOccurred(env);\n      (*env)->ExceptionClear(env);\n      if (!handle_exception(env, self, throwable)) {\n        fprintf(stderr, \"JNA: error while handling callback exception, continuing\\n\");\n      }\n      if (cif->rtype->type != FFI_TYPE_VOID)\n        memset(resp, 0, cif->rtype->size);\n    }\n    else {\n      extract_value(env, result, resp, cif->rtype->size, JNI_TRUE, cb->encoding);\n    }\n  }\n}\n\nstatic TLS_KEY_T tls_thread_data_key;\nstatic thread_storage* get_thread_storage(JNIEnv* env) {\n  thread_storage* tls = (thread_storage *)TLS_GET(tls_thread_data_key);\n  if (tls == NULL) {\n    tls = (thread_storage*)calloc(1, sizeof(thread_storage));\n    if (!tls) {\n      throwByName(env, EOutOfMemory, \"JNA: Can't allocate thread storage\");\n    }","sourceCodeStart":539,"sourceCodeEnd":575,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/native/callback.c#L539-L575","documentation":"When a Java callback throws, invoke_callback hands the throwable to handle_exception (the Callback.UncaughtExceptionHandler path). If that handler itself fails, JNA prints this message, has already cleared the exception, zeroes the native return buffer (for non-void returns), and continues the native call. It signals that both the callback and its exception handler failed.","triggerScenarios":"A Java Callback implementation throws, and the installed UncaughtExceptionHandler (or default handling via handle_exception) itself throws an exception or fails to run, right after CallObjectMethod sets a pending JNI exception.","commonSituations":"An uncaught exception handler that itself throws (e.g. logger misconfigured, NPE inside handler), exceptions thrown in callbacks invoked from native threads where class loading of the handler fails, or OOM inside exception handling.","solutions":["Fix the exception inside your callback so no exception ever reaches handle_exception.","Make the Callback's UncaughtExceptionHandler bulletproof: wrap its body in try-catch and never throw.","Check stderr/stdout of the process for the original exception stack trace printed before this message.","Ensure the callback class and its dependencies are loadable from the native callback thread's context classloader."],"exampleFix":"// before\nhandler = ex -> log.info(ex.getMessage()); // NPE if message null -> handler throws\n// after\nhandler = ex -> { try { log.warn(\"callback failed\", ex); } catch (Throwable t) { /* swallow */ } };","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// Install a handler that can never throw\ncallbackRef.setUncaughtExceptionHandler((cb, ex) -> {\n  try { LOGGER.log(Level.SEVERE, \"callback failed\", ex); }\n  catch (Throwable ignored) { }\n});","preventionTips":["Wrap all callback body logic in try-catch inside the Callback implementation","Never let logging/config code in an UncaughtExceptionHandler throw","Log the full stack trace at the callback boundary so root causes are visible","Test callbacks from native threads, not just Java threads"],"tags":["jni","callback","uncaught-exception"],"backgroundTag":"uncaught-exception-handler-failed","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}