Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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