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 trialFEI LI
828 Pointswhy the System.Exception won't catch it??
quiz question3 , I thought the System.Exception can catch all the other exceptions
5 Answers
Kyle Westendorf
6,564 PointsFor anyone who finds this in the future, I believe it is because TreeHouseDefense.OutOfBoundsException extends TreeHouseDefense.TreeHouseDefenseException, so this will catch before the more generic System.Exception.
Chris Jones
Java Web Development Techdegree Graduate 23,933 PointsHey FEI,
It does, but if you're catching a more specific exception first and that specific exception is thrown, then that catch block will be executed.
If you're catching multiple exceptions, it's common for the last CATCH block to be a the base Exception class, so that all other exceptions are caught in that CATCH block.
So, the quiz says that a TreehouseDefense.OutOfBoundsException is thrown by DoSomething()
in the code is below. Since the first catch block catches a TreehouseDefense.OutOfBoundsException and that is the exception that was thrown, then the code in that catch block will be executed.
If however, DoSomething() threw a IMadeThisUpException, then the last CATCH block would be executed, because the first two CATCH blocks do not catch IMadeThisUpException.
Does that help clarify?
try
{
DoSomething();
}
catch(TreehouseDefense.OutOfBoundsException ex)
{
}
catch(TreehouseDefense.TreehouseDefenseException ex)
{
}
catch(System.Exception ex)
{
}
FEI LI
828 PointsThe quiz question doesn't look right on my screen, here is what it looks....
If DoSomething() throws TreehouseDefense.OutOfBoundsException, which catch clause will catch it?
try
{
DoSomething();
}
catch(TreehouseDefense.TreehouseDefenseException ex)
{
}
catch(System.Exception ex)
{
}
Choose the correct answer below: Skip Quiz Review Video Get Help Next Question
A The exception won't be caught here.
B catch(TreehouseDefense.TreehouseDefenseException ex)
C catch(System.Exception ex)
Chris Jones
Java Web Development Techdegree Graduate 23,933 PointsHey FEI,
Are you saying you don't see the same three catch blocks that I have in my answer? You only see two catch blocks - one for TreehouseDefenseException and one for Exception?
FEI LI
828 PointsYes, thats what question3 said , I wish I can post the screenshot here, but I don't know how to do it...
Chris Jones
Java Web Development Techdegree Graduate 23,933 PointsHa! I see, there's more than one multiple choice question on the quiz - sorry :P!
Hmmm that's weird. I don't know why it's doing that. I would contact Support to make sure the quiz question is working correctly. I'm under the impression that you are correct - the catch(Exception ex)
block should catch the OutOfBoundsException in question #3.
FEI LI
828 PointsHmmm,I think there is a bug or something, I tried all three answers, and it says "catch(TreehouseDefense.TreehouseDefenseException ex)" is the right answer, I was very confused.....
Chris Jones
Java Web Development Techdegree Graduate 23,933 PointsYeah, I would contact Support about it. Definitely seems like a bug.