-1

Edited

I have a custom Exception class MyException

public class MyException extends RunTimeException{

    public MyException()
    {
       super();
    }

    public MyException(String message)
    {
        super(message);
    }

    public MyException(Exception e)
    {
        super(e);
    }
}

now whenever an exception is raised I catch it in MyException and throw it back to the parent function. I am facing an issue as I am using AsyncHttpClient and it raises an exception in onFailure as it is raised by the server.

This is how I am making HTTP Request and trying to handle Exception.

AsyncHttpClient client = new AsyncHttpClient();

    client.setTimeout(TIMEOUT);

    try {
        client.post(mainActivity,
                PATH, new StringEntity(requiredDataForRequest),
                "application/json", new AsyncHttpResponseHandler() {

                    @Override
                    public void onSuccess(String response) {

                    }

                    @Override
                    public void onFailure(int arg0, Header[] arg1,
                            byte[] arg2, Throwable e) throws MyException {
                        throw new MyException("Error Description Here!");
                    }

                });
    } catch (MyException e) {
        log.debug("Exception: " + e.getMessage());
    }

Now it is Crashing my app on throw new MyException and not catching it in catch block. Any idea or help would be appreciated.

Stephane Landelle
  • 6,990
  • 2
  • 23
  • 29
Muneeb Mirza
  • 810
  • 1
  • 17
  • 35

2 Answers2

0

Because the definition of onFailure does not specify that it throws anything means that none of its implementations can throw a checked exception. You can change your MyException by making it extend RuntimeException, but because it is a runtime exception any callers of onFailure are not required to catch the exception. If you are the only user of this class, that is fine as long as you remember to catch the exception yourself.

molenzwiebel
  • 886
  • 6
  • 21
  • i tried extending MyException to RuntimeException but i am unable to catch it. Eclipse doesn't even prompts me to add try catch block. Though I have added try/catch block manually arround the parent function but no use. – Muneeb Mirza Jan 12 '15 at 09:07
  • You can catch it just fine, only you are not *required* to (because it is a runtime exception). Note that this will throw an exception that the *caller* of `onFailure` needs to catch. If that is not you, throwing an exception would not invoke any of your catch statements. – molenzwiebel Jan 12 '15 at 10:14
  • can you please tell me who is the caller of onFailure? I have googled it but couldn't find the answer. I am new to this stuff. – Muneeb Mirza Jan 15 '15 at 06:19
0

You are missing the basic java concept here, you added a throw clause to your onFailure(), but the original method definition does not know what to do if something is thrown!

Basically when you use the throw keyword and throw an exception, then this exception has to be caught at the place where method is called.

Now, since you are overriding onFailure() and the AsyncHttpClient has no catch() for your MyException class, so its simply not getting catched!

If you want to get it catched, write the throw inside a try catch block in your onFailure()

try {
  throw MyException();
} catch(MyException e){
 // Exception caught
}

this will work as you have a catch for your thrown exception.

Next,In your case you call

try {
 client.post(///);
} catch (MyException e) {
  // exception caught
}

Now for the exception to be caught it has to be thrown by the client.post method call!

If you look at the post method

public RequestHandle post(android.content.Context context,
                 java.lang.String url,
                 org.apache.http.HttpEntity entity,
                 java.lang.String contentType,
                 ResponseHandlerInterface responseHandler)

It doesn't throws anything! so your exception will never be called!

And in case you want to catch something, you will either have to modify the library!

Rachit Mishra
  • 6,101
  • 4
  • 30
  • 51
  • I have added catch block for MyException, you can see the Updated Question. Please can you write the code snippet here? – Muneeb Mirza Jan 15 '15 at 07:48
  • you are not getting the point, in your catch block you can client.post, now the post method internally calls onFailure and onSuccess, now in your onFailure you throw an exception, but the onFailure doesn't know what to do if an exception is thrown, so it will now pass the exception to client.post() call, and your try block will never catch it! – Rachit Mishra Jan 15 '15 at 07:53
  • then where to add it? that IS my question. Where to add the catch block exactly... Point me out please. – Muneeb Mirza Jan 15 '15 at 07:57
  • and it would be really great if you re-write my code in your answer. Thanks – Muneeb Mirza Jan 15 '15 at 07:58
  • so what i want, can not be achieved. Thats what you wanted to say right? I can't re-throw exception from onFailure back to its parents? – Muneeb Mirza Jan 15 '15 at 08:11
  • No! Mr. Muneeb, unless you modify the library and its onFailure method, and then all upward calls, till your client.post call! – Rachit Mishra Jan 15 '15 at 08:15
  • In your code you have not rethrown the exception. Can you use throw new exception and tell me in CODE – Muneeb Mirza Jan 15 '15 at 08:21
  • No i haven't rethrown it because simply i can't do anything by rethrowing it! instead a catch it in onFailure itself and MyException class will do its work! – Rachit Mishra Jan 15 '15 at 08:23
  • then I am sorry, this is not what i have asked in my question. – Muneeb Mirza Jan 15 '15 at 09:44
  • i didnt ask initially for the code, but idea and help, i couldn't understand what you are trying to say thats why i have asked for code. Your exact answer to my question should be NO I CAN'T RE-THROW EXCEPTION. – Muneeb Mirza Jan 15 '15 at 10:32
  • That's not how stack works! An answer should be briefly detailed! – Rachit Mishra Jan 15 '15 at 11:06