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 Blog Reader Android App Getting Data from the Web Requesting Data from the Web the Wrong Way

Omar Tsai
Omar Tsai
1,704 Points

Why does catching exceptions fix errors in the app?

Why does catching exceptions fix errors? it Doesn't actually change anything so how come just by adding catch exceptions it fixes the problems? also i don't get "public static final String TAG = MainListActivity.class.getSimpleName();" What is the part behind the tag for?

1 Answer

Stone Preston
Stone Preston
42,016 Points

catching exceptions doesnt fix errors, it handles them. If you did not catch an exception, the exception would get thrown and the app would crash

android studio knows the code you are implementing can crash, so it wants to be sure you handle that by using a try/catch. if you dont android studio will give you an error.

By catching it, you are effectively saying " Hey this is an error that can happen, this is what I want to do when it happens"

catching exceptions means you anticipate that something can go wrong, and provide code to handle that.

Below is a scenario that could happen:

  1. you use a try catch block in your code

  2. the try code runs, but an exception is thrown.

  3. the catch "catches" the exception and you print out an error message for the user.

  4. the app does not crash since the exception was caught.

here is what would happen if you did not use a try catch:

  1. you run some code

  2. something goes wrong

  3. the exception is thrown but there is no catch to catch it, so the app crashes.