In your example, IOException and ServerException mean different things, so
they should be processed by different code paths..  if I'm understanding you
correctly.

If you don't like the sight of "instanceof" (I know I wouldn't), make it
possible for your Result to marshall either type of exception, properly
typed, by making two member variables, one for each exception type.

As for "every new exception" - I don't think you're going to have many of
them. Communication channel error vs. logical error should already cover it,
I would think.
26.04.2011 20:41 пользователь "dnkoutso" <[email protected]> написал:
> Hey everyone,
>
> I was looking for an idiom or paradigm on how to handle HTTP errors.
>
> Basically,
>
> I have an AsyncTask that runs in a background thread, that calls a static
> method of mine called executeRequest().
>
> This is all done in the doInBackground() part. There are two types of
> exceptions that be thrown by executeRequest(). An IOException for all
> communication errors and a ServerErrorResponse exception which is my own
> exception. This can occur if for example the client sent something bad to
> the server, the whole HTTP worked but the server complained (perhaps I
> passed an invalid parameter or id).
>
> So, what I have done is wrap the result in a "result object" of my own.
>
> In onPostExecute() I check if the result was failed and then I try to
handle
> it in the UI thread. However, I now have to start doing
>
> Excpetion e = result.getException();
> if (e != null) {
> if (e instanceof IOException) //network error {
> //handle network error here
> } else if (e instanceOf ServerErrorResponseException) {
> //handle server error response here
> }
>
> As you can see this is becoming annoying, For every new exception I have
to
> check it using instanceof. Is there a way around it or a design I could
> follow to avoid this? I want the excpetions to be processed in the UI
thread
> in case I show a dialog box or something to the user.
>
> Thanks!
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to