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 trialbillwerba
8,283 PointsError: 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
Sara Chicazul
7,475 PointsThe 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
Courses Plus Student 1,194 PointsAshley Ewen
Courses Plus Student 1,194 PointsI had this same problem, was using toString() instead of string(). Thanks!