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 (2015) Concurrency and Error Handling Making Our Code Asynchronous

Error: exception IO Exception is never thrown in body of try statement

Catch statement's "IOException e" is red underlined, what's the problem:

@Override public void onResponse(Response response) throws IOException { try { if(response.isSuccessful()) { Log.v(TAG, response.body().toString()); } } catch (IOException e) { Log.e(TAG, "Exception Caught", e); } }

1 Answer

The problem is that an IOException is a specific type of exception, and nothing in your try block would create that particular exception. You have done one little thing different from the example:

// YOUR CODE
// Uses Java toString() method, does not throw IOException
// May throw a different type of exception!
Log.v(TAG, response.body().toString());

// ORIGINAL CODE
// Uses string() method of okhttp/ResponseBody object, throws IOException
Log.v(TAG, response.body().string());

It's a bit confusing that the ResponseBody object has a method that is so similarly named to a standard Object method! It does do something different than toString() though. You can see the code on GitHub.

Ashley Ewen
Ashley Ewen
Courses Plus Student 1,194 Points

I had this same problem, was using toString() instead of string(). Thanks!