Exceptionhandling preserves the original failure as an inner exception and reports the outer error to the user.
Exception handling preserves the original failure as an inner exception and reports the outer error to the user.FindItem method, after looping through the list, check to see if the result is null.If so, throw a new exception with the following message:
$"An item of type {type.Name} does not exist."MoneyCollectingBooth's SellTicket method, surround the code that finds the item and adds the money in a try...catch that catches Exceptions.In the catch block, throw a NullReferenceException, passing in a message and an inner exception, as shown in the code below:
catch (Exception ex)
{
throw new NullReferenceException("Ticket not found.", ex);
}FindItem method (in the sell water bottle/give coupon or map methods) with a try...catch block that catches Exceptions and throws a NullReferenceException that uses the caught exception as the inner exception.
SellTicket, AddGuest, and PopulateGuestListBox are surrounded by a try/catch that catches NullReferenceExceptions.This is where the exceptions thrown in the FindItem methods will be caught.
Exception object) is that of the NullReferenceException type.Notice that it has an InnerException, which contains the values of the Exception type thrown in the FindItem method.

MessageBox appears displaying the exception message.MissingItemException with the specified information.BoothItems project, create a new class called MissingItemException as specified in the diagram below.Pass the message parameter into the call to base. Leave the constructor empty.

FindItem method, throw a MissingItemException instead of an Exception. Keep the message the same.FindItem, catch a MissingItemException instead of an Exception.MessageBox.Show(ex.Message + "\n" + ex.InnerException.GetType().ToString() + ": " + ex.InnerException.Message);MissingItemException instead of type Exception.
Ensure that a MessageBox pops up with the custom exception type and a more robust message about what is going wrong.
MainWindow.AnimalWindow, passing in the animal. Then show the window.DialogResult is true, repopulate the animal list box.Add a Dingo animal to the zoo in the window_Loaded event handler in the MainWindow, so you don't have to create a new object every time the application loads
The animal window should appear, and its controls should be initialized to the animal's values.
Then double click in the background of the list box. Nothing should happen.
name or money balance.Make all of the new animal properties auto-properties (without fields behind them). Put the two enumerations in the Animals project.

Example: public int MoveDistance { get; set; }XPosition: 0XPositionMax: 800XDirection: HorizontalDirection.LeftVerticalDirection.UpMoveDistance to 10.Move method.if (this.XDirection == HorizontalDirection.Right)
{
if (this.XPosition + this.MoveDistance > this.XPositionMax)
{
this.XPosition = this.XPositionMax;
this.XDirection = HorizontalDirection.Left;
}
else
{
this.XPosition += this.MoveDistance;
}
}
else
{
if (this.XPosition - this.MoveDistance < 0)
{
this.XPosition = 0;
this.XDirection = HorizontalDirection.Right;
}
else
{
this.XPosition -= this.MoveDistance;
}
}MainWindow called moveAnimalButton. Add a click event handler to it.If the animal is not null and is a mammal, call the animal's Move method.
Move method.Move animal button.XDirection: LeftXPosition: 0MoveDistance: 10XPositionMax: 800XPosition to 0 and the XDirection to Right.Move animal button again.XDirection should now be Right.MoveDistance to the XPosition. XPosition should now be 10.Copy and paste the code in the file into the App.xaml file. All of the code should go inside the Application.Resources tags.

CageWindow in the ZooScenario project.Add an animal field and parameter as specified in the diagram below.

It has the following properties with corresponding values:
"animalGrid"Margin:"0,0,0,45"moveAnimalButton and its click event handler from the MainWindow to the CageWindow.The button should be inside the outer grid and below the inner grid.

Move method.MainWindow called showCageButton. Add a click event handler for it.If the animal is not null, create a CageWindow and show it using .Show(). Otherwise, tell the user that they must select an animal to show.
Move method.Show cage button. The CageWindow should appear.Move animal button.The breakpoint in the mammal's Move method should get hit. The method should have the same functionality as before.
CageWindow.CageWindow as specified in the diagram below.
DrawAnimal method from the window_Loaded event handler.CageWindow.xaml below the existing Grid tags.<Grid x:Name="outerGrid"> <Viewbox Stretch="Uniform"> <Border x:Name="cageBorder" BorderBrush="Black" BorderThickness="1" Height="400" Width="800" Margin="10,10,10,50"> <Grid x:Name="cageGrid"/> </Border> </Viewbox> </Grid>
moveAnimalButton xaml tags below the end Viewbox tag.Height of the CageWindow to 500 and the Width to 900.Your xaml code should look similar to the image below.

GetViewBox method// Instantiate viewBox.
Viewbox finishedViewBox = new Viewbox();
// Sets the animals size.
double itemWidth = canvas.Width;
double itemHeight = canvas.Height;
// Sets the screen scale.
double xScale = 0.7;
double yScale = 0.7;
xScale *= this.cageGrid.ActualWidth / maxXPosition;
yScale *= this.cageGrid.ActualHeight / maxYPosition;
// Changes the size of the animal based on the scale of the screen.
itemWidth *= xScale;
itemHeight *= yScale;
// Sets the width of the viewBox to the size of the canvas.
finishedViewBox.Width = itemWidth;
finishedViewBox.Height = itemHeight;
// Sets the animals location on the screen.
double xPercent, yPercent;
// Scales based on position - on maxX and maxY
xPercent = (this.cageGrid.ActualWidth - itemWidth) / maxXPosition;
yPercent = (this.cageGrid.ActualHeight - itemHeight) / maxYPosition;
int posX, posY;
posX = Convert.ToInt32(xPosition * xPercent);
posY = Convert.ToInt32(yPosition * yPercent);
finishedViewBox.Margin = new Thickness(posX, posY, 0, 0);
// Adds the canvas to the view box.
finishedViewBox.Child = canvas;
// Returns the finished viewBox.
return finishedViewBox;DrawAnimal methodcageGrid. Otherwise, the animal would be redrawn over and over and over without ever being removed.this.cageGrid.Children.Clear();resourceKey to the Name of the animal's Type. Using a ternary operator, if the animal's age is zero add 'Baby' to the end of the resourceKey, else add 'Adult'.Animal XAML and store it as a Canvas object. Create a Canvas variable called canvas. Set the variable to the code below:Application.Current.Resources[resourceKey] as Canvas;Viewbox variable to the GetViewBox method. This viewBox will be placed in the cageWindow's grid.GetViewbox method call:maxXPosition: 800maxYPosition: 400xPosition: animal.XPositionyPosition: animal.YPositionviewBox to the grid programmatically by setting the viewBox's HorizontalAlignment to the left and set the viewBox's VerticalAlignment to the top. It will look like the code below.viewBox.HorizontalAlignment = HorizontalAlignment.Left;
viewBox.VerticalAlignment = VerticalAlignment.Top;viewBox is aligned.// If the animal is moving to the left
if (animal.XDirection == HorizontalDirection.Left)
{
// Set the origin point of the transformation to the middle of the viewBox.
viewBox.RenderTransformOrigin = new Point(0.5, 0.5);
// Initialize a ScaleTransform instance.
ScaleTransform flipTransform = new ScaleTransform();
// Flip the viewBox horizontally so the animal faces to the left
flipTransform.ScaleX = -1;
// Apply the ScaleTransform to the viewBox
viewBox.RenderTransform = flipTransform;
}viewBox is not attached to the grid, so it won't be seen when the Show Cage button is clicked. To add the viewBox to the grid, paste the code below at the end of DrawAnimal.this.cageGrid.Children.Add(viewBox);CageWindow called drawAnimalButton.Add a click event handler for it. In the event handler, call the DrawAnimal method.
Animal constructor, assign the YPosition to 400 instead of 0 to make the animal start at the bottom not the top.Show cage button. The CageWindow should appear and the dingo should be drawn.Move animal button, and then click the Draw animal button. The dingo should move a small amount to the right.DrawAnimal method that Clear's the cageGrid's Children and in the Animal class's constructor change the MoveDistance from 10 to 40.Show Cage.Move animal button several times and then the Draw animal button. The Dingo should move farther than before and now there should be two Dingos instead of one.Move animal button and Drawn animal button until the Dingo reaches the end. Ensure that the Dingo flips around when it reaches the right side.DrawAnimal method that Clear's the cageGrid's Children and in the Animal class's constructor change the XPositionMax to 850.Show Cage.Move animal button 22 times (sorry) and then the Draw animal button. Notice that the Dingo is outside of the bounds of the cageWindow.XPositionMax back to 800.CageWindow.xaml, comment out the Viewbox tags and remove the Height and Width property from the cageGrid.Show cage button.Viewbox tags and replace the Height (400) and Width (800) properties.cageGrid should scale correctly. The buttons, however, are still an issue but we will get rid of those in the next step.Animal class as specified in the diagram below.Use the System.Timers namespace when adding the timer.

Animal constructor, initialize the move timer to a new timer.Pass in 1000 milliseconds for the interval, as shown in the code below:
this.moveTimer = new Timer(1000);MoveHandler method to the timer's Elapsed event, as shown in the code below.this.moveTimer.Elapsed += this.MoveHandler;MoveHandler method, call the Move method.moveAnimalButton and its click event from the CageWindow.MoveDistance from 40 back to 10.The animal should move across the window. Wait a few seconds and click the button again. The animal should move farther across the window.
Animal class MoveHandler method.The debugger will continually kick you back to the breakpoint, making it virtually impossible to debug.
A blue circle will appear next to the yellow arrow as seen in the image below.

Animal class MoveHandler method, disable the move timer prior to calling the Move method using .Stop(), then enable the timer after calling Move using .Start()Animal class MoveHandler method.Surround the line stopping the timer with a conditional compiler directive to run only in debug mode. Do the same for the starting of the timer.
CageWindow.xaml.cs as specified in the diagram below.
RedrawHandler method to the timer's Elapsed event.RedrawHandler method.This prevents threading issues between the timer and the drawing.
5.1.9 How to Avoid Threading Issuesthis.Dispatcher.Invoke(new Action(delegate()
{
this.DrawAnimal();
}));drawAnimalButton and its click event handler from the CageWindow.Margin of the cageBorder to 10,10,10,10Animal and CageWindow) and the MoveDistance of the animal.The gif below has a MoveDistance of 20, moveTimer of 100 milliseconds and a redrawTimer of 10 milliseconds.

Mammal class' Move methodAnimal editingAnimal's age to 0 and show it again.Animal's portrait should be a baby version and it should still be moving like before.Animal in the zoo.Canvas© 2026 Northcentral Technical College