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
Jake Nagle
2,942 PointsNeed a little help with the logic of catch/try and exceptions...
I'm working on the weather app project and need some review with try/catch blocks and throwing exceptions. Basically, I've made this code:
public void onResponse(Response response) throws IOException {
try {
Log.v(TAG, response.body().string());
if (! response.isSuccessful()) {
alertUserAboutError();
}
} catch (IOException e) {
Log.e(TAG,"Exception caught: ", e);
}
}
And it works, but I don't understand how/why. Can you break down what's going on? Also, I've noticed that taking out the Log.v line creates an error. Why is that?
Thank you very very much!
1 Answer
Ozhan Saat
Courses Plus Student 4,002 PointsIf you take out the Log.v line then its not trying anything. Try to read it like a story.
Try this action If no response (! resonse.isSuccessful) Alert the user about the Error
The catch block is there to catch the exception so your app doesn't crash. The app is expecting a response and when it doesn't get one it throws an exception. This can crash your app but instead you contain it and give the user a error message. An example could be trying to open a web page when you're not connected to the internet.
Jake Nagle
2,942 PointsThis makes sense. Thanks very much!
Ben Deitch
Treehouse TeacherBen Deitch
Treehouse TeacherI added the word java after your first set of 3 backticks. That's the secret to making the code coloring appear. What error does taking our the Log.v line create?