Android Build a Weather App Concurrency and Error Handling Configuring the AlertDialog

remi poolen
remi poolen
5,434 Points

error: exception IOException is never thrown in body of corresponding try statement

i followed along with the video, making a weather app. but on the onResponse method of a CallBack i get the above error.

Code:

    Call call = client.newCall(request);
    call.enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {

        }

        @Override
        public void onResponse(Call call, Response response) throws IOException  {
            Log.v(TAG, response.body().string());
            try {
                if (response.isSuccessful()) {

                }
                else {
                    alertUserAboutError();
                }
            } catch (IOException e) {
                Log.e(TAG, "IO Exception caught", e);
            }
        }
    });

1 Answer

Martin Klestil
Martin Klestil
5,235 Points

It's a small bug, he can not write a log because the tag only exists locally in the if block. Copy Log.v (TAG, response.body (). String ()); in front of the if block.

I hope this helps you.

@Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    Log.v(TAG, response.body().string());
                    if (response.isSuccessful()) {

                    }
                    else{
                        alertUserAboutError();
                    }
                } catch (IOException e) {
                    Log.e(TAG, "IO Exception caught: ", e);
                }
            }