Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

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,520 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);
                }
            }