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<noob />
17,062 Pointsfew questions about this video.
hi, Im not fully understand why we created the Exception.cs file in the first place? for what purpose?
another question is, in this example the teacher write this code :
class TreehouseDefenseException : System.Exception
{
public TreehouseDefenseException()
{
}
public TreehouseDefenseException(string message) : base(message)
{
}
}
class OutOfBoundsException : TreehouseDefenseException
{
public OutOfBoundsException()
{
}
public OutOfBoundsException(string message) : base(message)
{
}
}
}
TreehouseDefenseException inherits from the system.exception, But why after that "OutOfBoundsException inherits TreehouseDefenseException?
another thing i dont fully understand is the meaning of "base" why in this example we passed ":base(message) 2 times?
1 Answer
Allan Clark
10,810 PointsFirst the point of creating TreehouseDefenseException and all is to show you an example of how to extend the Exception class and create your own exceptions. The 'in code' reason is to be able to differentiate this type of exception from other Exceptions that may be thrown by the system or other projects. Also it gives you the ability to handle this specific type of exception differently when you are setting up a try-catch block. So for example if only for TreehouseDefenseException you just wanted to log the error and continue running, but normal exceptions you wanted to display a message to the user.
try
{
//some code that may throw either a TreehouseDefenseException or some other Exception
} catch(TreehouseDefenseException tdex) {
//just log error
}catch(Exception ex) {
//display some message to the user
}
As far as why OutOfBoundsException inherits TreehouseDefenseException, it seems the idea here is to begin creating a list of custom exceptions that are all identified as being thrown by the Treehouse Defense code, so that these exceptions can be handled differently that regular system exceptions. (again aside from the educational purpose)
The base() call is used in the method signature of a constructor when you want to send a parameter to the constructor of the parent class. It is used twice in this code because there are 3 levels of exceptions. So whenever a new OutOfBoundsException is created and thrown a message parameter can be sent into the constructor (though not required since there is a default constructor), after any code in the constructor method is run, the same message parameter is passed up to the constructor of the TreehouseDefenseException class through the base(message) call. Then the same thing happens in TreehouseDefenseException to pass the same message parameter up to the top parent class's constructor System.Exception.
Hope this helps. Please let me know if I can clarify anything. And happy coding :)
<noob />
17,062 Pointsthanks for ur time to answer my question, i hope as i continue the course this will make more sense.
Steven Parker
231,261 PointsSteven Parker
231,261 PointsHi, I got alerted by your tag, but it looks like Allan has already provided a thorough answer.
Happy Holidays! And for some fun and extra coding practice, give Advent of Code a try!