1

I have a class (named C) to check the structure of JSON and if the structure is wrong, it will throws exception. That class will be called from class B. I can use try catch in class B. But actually, class B is called by class A, which provide callback to class B. How do I send exception from class C which is called by class B to the callback (OnFinishListener) in class A? I want to provide try catch in the callback declaration in class A, not class B.

public class A {
    B b = new B();
    b.getResponseFromServer(new B.OnFinishListener {
        @Override
        public void onFinish(Object response) {
    });
}

public class B {
    public static interface OnFinishListener {
        void onFinish(Object response);
    }

    //other code

    public void getResponseFromServer(OnFinishListener onFinishListener) {
    Response.Listener<JSONObject> listener = new Response.Listener<>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    onFinishListener.onFinish(C.someMethod(response);
                catch (JSONException e) {
                    Log.e("Error", e.getMessage());
                }
            }
        } 

        //do some Volley tasks
    }
}

public class C {
    public static Object someMethod(JSONObject json) throws JSONException {
        if (json.has("something")) {
            //return something
        } else {
            throw new JSONException("Error");
        }
    }
}
Android Develeoper
  • 411
  • 1
  • 6
  • 24
stackex
  • 3,205
  • 1
  • 11
  • 16

2 Answers2

4

I got the solution. I just need to add one method to my interface (onException) and deliver the exception to this interface on catch(Exception).

public class A {
    B b = new B();
    b.getResponseFromServer(new B.OnFinishListener {
        @Override
        public void onFinish(Object response) {}

        @Override
        public void onException(JSONException response) {}
);
}

public class B {
    public static interface OnFinishListener {
        void onFinish(Object response);
        void onException(JSONException e);
    }

    //other code

    public void getResponseFromServer(OnFinishListener onFinishListener) {
 Response.Listener<JSONObject> listener = new Response.Listener<>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    onFinishListener.onFinish(C.someMethod(response);
                catch (JSONException e) {
                    onFinishListener.onException(e);
                }
            }
        } 

        //do some Volley tasks
    }
}
stackex
  • 3,205
  • 1
  • 11
  • 16
-1

Try ur code to put in Try Catch...

try {
      if (json.has("something")) {

      } else {

      }
    }catch (JSONException e) {
        e.printStackTrace();
    }
Android Develeoper
  • 411
  • 1
  • 6
  • 24
  • yes, I know I can do that. but, I don't want to `try catch` in Class C. That's why I `throws JSONException` in Class C. My code above use `try catch` in Class B which is fine too. But, I want to `try catch` in Class A, not in Class B or Class C. The problem is how do I send the `Exception` to Class A? – stackex Apr 23 '15 at 05:58
  • oky i think this can help u i hope so..... [http://stackoverflow.com/questions/9510409/how-to-throw-an-exception-from-a-class-back-to-the-form-that-called-it] – Android Develeoper Apr 23 '15 at 07:04