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

tariqasghar
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
tariqasghar
UX Design Techdegree Graduate 30,519 Points

try catch IOException and throws IOException on the same method.

In the "Introducing JSONObject" video on "Build a weather app" course in android track, the onResponse method throws IOException but is also catching the IOException. Shouldn't the method calling onResponse catch the IOException??

1 Answer

Seth Kroger
Seth Kroger
56,412 Points

The method calling onResponse() is somewhere in the depths of the okhttp library. Just letting the library deal with it may sound okay but there was still an error in getting the forecast. It's better for our app deal with the error and take the appropriate action instead of ignoring it. Catching the IOException allows us to do that.

tariqasghar
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
tariqasghar
UX Design Techdegree Graduate 30,519 Points

I asked this question because in the same video we are creating another method "getCurrentDetails" that throws json exception. The method calling it "onResponse" is catching the json exception not the method that has a throws keyword (getCurrentDetails).