Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6891403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:24:19+00:00 2026-05-27T06:24:19+00:00

Suppose I have a Java class like this: public class Test { static {

  • 0

Suppose I have a Java class like this:

    public class Test
    {
        static { System.loadLibrary("test"); }
        public native int foo();
    }

Suppose that the foo() method is doing some JNI calls, and one of these calls fails (IE, throws an exception). I would then like to return from the JNI code and have the exception thrown in Java. For example:

    jint Java_Test_foo(JNIEnv* env, jobject thiz)
    {
        jstring foobar = (*env)->NewStringUTF(env, "Hello from JNI !");
        if(foobar == NULL) // Not enough memory. OutOfMemoryError is thrown
            return NULL; // Return immediately to get exception thrown in Java
        // Do other stuff
        ...
        return some_computed_jint;
    }

The problem is that return NULL is not a jint. In Android for example, I would get this warning when compiling:
warning: return makes integer from pointer without a cast.

Now the question is: What should I return in case of an Exception being thrown inside a JNI method that returns a jint?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:24:20+00:00Added an answer on May 27, 2026 at 6:24 am

    If your code (or a library) raises an Exception in Java, it doesn’t matter what value you return, Java will ignore it. Obviously it needs to be a compatible type – so returning 0 in your example would seem to make sense, or whatever you’re comfortable with. When your code returns, the Java runtime will notice that an Exception has been raised and continue to propagate it and ignore the return value of your function.

    You will, of course, need to return a compatible type. Don’t simply return NULL, as that will be cast to an int when the function is not declared to return a pointer, which may not be appropriate.

    Obviously, though, when you’re calling C functions, those would not raise an Exception. So you can either map an integer to an error condition (-1 for example) and then throw the Exception in Java, or you can take the time to build an Exception in JNI.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose that I have a Java class with a static method, like so: class
Suppose I have a simple Java class like this: public class User { String
Say suppose I have the following Java code. public class Example { public static
I've seen examples like this: public class MaxSeconds { public static final int MAX_SECONDS
Suppose that I have a Java program within an IDE (Eclipse in this case).
I have a class like this public SomeClass { private List<string> _strings = new
Suppose I have an abstract class FactorizedDialog . It looks like this (please note
Suppose we have an arbitrary graph represented by nodes and pointers like this: class
Suppose I have a Java method that returns a HashMap object. Because a LinkedHashMap
I'm working on a Java project where I have created a class that looks

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.