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

C# C# Objects Inheritance Catching Exceptions

FEI LI
FEI LI
828 Points

why the System.Exception won't catch it??

quiz question3 , I thought the System.Exception can catch all the other exceptions

5 Answers

Kyle Westendorf
Kyle Westendorf
6,564 Points

For 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
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Jones
Java Web Development Techdegree Graduate 23,933 Points

Hey 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
FEI LI
828 Points

The 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
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Jones
Java Web Development Techdegree Graduate 23,933 Points

Hey 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
FEI LI
828 Points

Yes, thats what question3 said , I wish I can post the screenshot here, but I don't know how to do it...

Chris Jones
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Jones
Java Web Development Techdegree Graduate 23,933 Points

Ha! 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
FEI LI
828 Points

Hmmm,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.....