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 trialOmar Tsai
1,704 PointsWhy 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
42,016 Pointscatching 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:
you use a try catch block in your code
the try code runs, but an exception is thrown.
the catch "catches" the exception and you print out an error message for the user.
the app does not crash since the exception was caught.
here is what would happen if you did not use a try catch:
you run some code
something goes wrong
the exception is thrown but there is no catch to catch it, so the app crashes.
Omar Tsai
1,704 PointsOmar Tsai
1,704 PointsThanks