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) Working with JSON Introducing JSONObject

boog690
boog690
8,987 Points

Code isn't DRY

In the video, Ben Jakuben adds Log.e(TAG, "Exception caught: ", e); to both the IOException catch block and the JSONException catch block.

Well, my question is, how do we make this DRY? I tried:

Log e = Log.e(TAG, "Exception caught: ", e);

outside the try/catch block but e obviously isn't defined at that point. Where do I go from here?

2 Answers

The only way to make this DRY would be to define a function called, say, catchException and call that from both places.

There's little point as the line of code doesn't actually bring much to the party; it just logs the error, should one occur, in the system output.

public void catchException(e) {
    Log.e("exception caught: ", e)
}

That might work but so would deleting both lines. ;-)

Mohammed Amin
Mohammed Amin
5,445 Points
catch (IOException | JSONException e) { 
     Log.e(TAG, "Exception caught: ", e)
}

is something you can try.