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 Handling Errors

Rex Wang
Rex Wang
2,089 Points

Why would`respose#isSuccessful()` throw an exception?

This sounds a bit counter intuitive to me. If the method is supposed to return true or false and doesn't actually do anything other than say the give the state of the application, does it make sense to throw an exception?

2 Answers

Hello,

I can't say offhand what the isSuccessful method is doing that it could throw an IOException, but in general, a lot of things could cause an exception, even in a method that only returns a boolean. For example, a method could return true if a file has the word Treehouse in it, and thus need to read the content from a file, but if that file doesn't exist or doesn't terminate correctly, an IOException would then be thrown. I hope this helps to clairify things, if not, please let me know and I can try to clairfy further.

Boris Rosenow
Boris Rosenow
3,218 Points

Actually isSuccessful does not throw an IOException. I'm afraid Ben made a tiny mistake here. The try/catch block was there in the first place to catch an IOException thrown by call.execute. If you look at the documentation you can see it actually saying as much.

It was an honest mistake though, as we deleted that call.execute line early at some point and left the try/catch block there.