Handle Java plugin exception from C#?

e.g.

Android section

public class MainActivity extends UnityPlayerActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  }

  public int divide(int a, int b) throw Exception{
    return a / b;
  }
}

Answers


First of all, jo.Call<int>("divide", new object[](10, 0)); should not even compile. That should be jo.Call<int>("divide", new object[] { 10, 0 });.

But, can the exception be catched in Unity3D code?

No

If there is an exception thrown from the Android native side, your app will be killed.

For example, the code below:

AndroidJavaClass jc;
AndroidJavaObject jo;
int rlt;
try
{
    jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
    rlt = jo.Call<int>("divide", new object[] { 10, 0 });
}
catch (Exception e)
{
    Debug.Log("Error: " + e.Message);
}

will only catch an exception if the return type or parameter does not match or even when the divide function does not exist at-all.

You have to catch the Exception from the Java side.

public int divide(int a, int b)
{
    int result = 0;
    try
    { 
        result = _divide(a, b);
    }catch(Exception e)
                      {
      //...
    }
    return result;
}

public int _divide(int a, int b) throw Exception{
    return a / b;
}

You can change the function return type to string, catch the exception, convert the exception into a json with JSONObject then return result to C# and get each data by using JsonUtility


Need Your Help

How to obtain HttpServletRequest in AppservPasswordLoginModule (SSL)

authentication glassfish jaas

In a customized Login Module I've developed for my application server (GlassFish 3.1.2.2), I'm using the following syntax to obtain the HttpServletRequest:

Perl: How to go to the root directory from a script

perl directory root

I want to go to a directory, print in some files, then go back to the root directory.