4.3 Scratchpad Learning Activity

Overview

4.3 Scratchpad Learning Activity

This assignment covers Error Handling, String Manipulation, Exception Types and Throwing Exceptions

Work through the stories in order. Use the acceptance criteria to check each feature, and complete the nested tasks using the specified names, values, and scenarios.

If you would like, you can follow along with instructor-led lab videos.
1.If you would like, you can follow along with instructor-led lab videos.

4.3 Scratchpad lab video, part 1 (22:29)

4.3 Scratchpad lab video, part 1 (22:29)

4.3 Scratchpad lab video, part 2 (11:14)

4.3 Scratchpad lab video, part 2 (11:14)
A If you would like, you can follow along with instructor-led lab videos.
Add a new case for Parking Cost.
Acceptance Criteria
Acceptance criteria
  • The parking command assigns the required parking cost on the existing airport.
  • The specified initial null-reference scenario is observed before error handling is added.
Instructions
2.Add a new case for Parking Cost.
Command Diagram: Parking; Members and relationships are shown in the editable reference; unrelated members may be omitted.
Command Diagram: Parking
  • New
  • Changed
  • Removed
A In the Program class of the ScratchpadConsole project above the line of code that assigns the Title, initialize a local…

In the Program class of the ScratchpadConsole project above the line of code that assigns the Title, initialize a local variable of type Airport named dfw and assign it to null.

B In the "new" case, remove the type from the dfw variable.
C In the Airport class in the Airports project, add a new private field named parkingCost of type decimal.
D Wrap the parkingCost field with a property named ParkingCost.
E In the Program.cs, add a new case to the switch statement for "parking".
F In the "parking" case, assign the *dfw*'s ParkingCost to 10.50m.
Add error handling to the parking case.
Acceptance Criteria
Acceptance criteria
  • Run the application and type enter 'parking' as a command. The breakpoint should be hit.
  • Press Continue or F5. The message you wrote should be displayed in the console and the while loop should start over again.
  • The completed implementation satisfies this requirement: Add a try statement around the code that we want to protect from throwing a NullReferenceException. In this case it's the line of code that assigns the ParkingCost property.
Instructions
3.Add error handling to the parking case.
A Run the application and enter 'parking' as a command. You should get a NullReferenceException because the dfw object has not…

Run the application and enter 'parking' as a command. You should get a NullReferenceException because the dfw object has not been instantiated first.

B In the NullReferenceException box, click the 'View Details' button.

A NullReferenceException is an object. Because it's an object it has its own properties that we can use as shown in the image below. We will be looking at this in more detail in a later step.

Snip: NullReferenceException Object for 4.3 Scratchpad Learning Activity (NEW). Use the adjacent task instructions to identify the required controls, output, or debugger values.
Snip: NullReferenceException Object
C Add a try/catch statement around the ParkingCost code.
1 Add a try statement around the code that we want to protect from throwing a NullReferenceException. In this case it's the…

Add a try statement around the code that we want to protect from throwing a NullReferenceException. In this case it's the line of code that assigns the ParkingCost property.

Snip: Try for 4.3 Scratchpad Learning Activity (NEW). Use the adjacent task instructions to identify the required controls, output, or debugger values.
Snip: Try
2 Below the try, add a catch statement. This is where we can handle whatever runtime error occurs.
Snip: Catch for 4.3 Scratchpad Learning Activity (NEW). Use the adjacent task instructions to identify the required controls, output, or debugger values.
Snip: Catch
3 Inside the catch statement, do a Console.WriteLine with the text "Please create an airport before setting its parking cost".
4 Check Your Work:

Set a breakpoint at the opening curly brace of the try statement.

Run the application and type enter 'parking' as a command. The breakpoint should be hit.

Press F10 over the line of code that assigns the ParkingCost. Instead of a NullReferenceException being generated it will jump to the catch statement instead.

Press Continue or F5. The message you wrote should be displayed in the console and the while loop should start over again.

This error handling has kept the application from crashing and allows the user to try again. An application should NEVER crash on a user.

Allow the console to handle more than one word as a command.
Acceptance Criteria
Acceptance criteria
  • Press F10. The commandWords array should have 1 string in it.
  • Press F10. The debugger should move to the try statement in the "parking" case.
  • Step over the line of code that assigns the ParkingCost. The ParkingCost should successfully be assigned to 10.50.
Instructions
4.Allow the console to handle more than one word as a command.

In this step you will be taking multiple words and parsing (to separate) them for different uses. This assumes that the commands being entered do not have extra spaces, commas, etc.

Command Diagram: parkingCost "parameter"; Members and relationships are shown in the editable reference; unrelated members may be omitted.
Command Diagram: parkingCost "parameter"
  • New
  • Changed
  • Removed
A Change the command variable to line so it better represents what is being input (a bunch of things vs one single thing).
B After the Console.ReadLine, define a string array named commandWords and assign it to the Split method called on the line variable.
string[] commandWords = line.Split();

string[] means that this is an array and the type of things IN the array are strings.

Split() takes the words in the line variable and converts them into an array based on spaces by default. This is called an extension method and will be covered later in the course.

C In the switch statement, we only care about the first word so replace the condition with commandWords[0].
Snip: CommandWords[0] for 4.3 Scratchpad Learning Activity (NEW). Use the adjacent task instructions to identify the required controls, output, or debugger values.
Snip: CommandWords[0]

Arrays are 0 based just like lists to the first item in an array is going to be at 0. The bracket notation is used to identify the place in the array.

D Replace command in the default case with comandWords[0] as well.
E Assign the ParkingCost by changing the harcoded value to to the second word in the array.
dfw.ParkingCost = decimal.Parse(commandWords[1]);

decimal.Parse() takes a string and returns its decimal equivalent.

F Check Your Work:
1 Set a breakpoint on the line that calls Split.
2 Run the application and type in 'new'.
3 Press F10. The commandWords array should have 1 string in it.
Snip: New for 4.3 Scratchpad Learning Activity (NEW). Use the adjacent task instructions to identify the required controls, output, or debugger values.
Snip: New
4 Press F5 to continue and then enter 'parking 10.50' into the console.
5 Press F10. Note that the line variable is "parking 10.50", but the commandWords array has two strings: "parking" and "10.50".
Snip: Parking for 4.3 Scratchpad Learning Activity (NEW). Use the adjacent task instructions to identify the required controls, output, or debugger values.
Snip: Parking
6 Press F10. The debugger should move to the try statement in the "parking" case.
7 Step over the line of code that assigns the ParkingCost. The ParkingCost should successfully be assigned to 10.50.
Snip: ParkingCost for 4.3 Scratchpad Learning Activity (NEW). Use the adjacent task instructions to identify the required controls, output, or debugger values.
Snip: ParkingCost
Create better messages for potential user mistakes.
Acceptance Criteria
Acceptance criteria
  • The completed implementation satisfies this requirement: Set a breakpoint on the opening curly brace of the catch statement and run the application.
  • The completed implementation satisfies this requirement: Remove the ex variable from the NullReferenceException catch; if you aren't using it you don't need it.
  • The completed implementation satisfies this requirement: Add an additional catch statement after the first catch statement that catches a FormatException type.
Instructions
5.Create better messages for potential user mistakes.

Currently, if users type in anything other than 'parking 10.50' they will get the error message about creating an airport because our catch statement is generic and if anything fails that's the message the user will get. This isn't a helpful message since the user might actually have created an airport and if they have this makes no sense. In this step you will explore different ways of handling exceptions thrown by the application.

A Catch all exceptions.
1 After the catch keyword in the try/catch, add (Exception ex). What this does is catch any exception thrown in the try…

After the catch keyword in the try/catch, add (Exception ex). What this does is catch any exception thrown in the try statement and stores that exception object in the ex variable.

Snip: Exception for 4.3 Scratchpad Learning Activity (NEW). Use the adjacent task instructions to identify the required controls, output, or debugger values.
Snip: Exception
2 Set a breakpoint on the opening curly brace of the catch statement and run the application.
3 Enter the commands 'new' and 'parking ten'.
4 Expand the ex object the autos window. Note that the type of exception is shown and the Message property contains…

Expand the ex object the autos window. Note that the type of exception is shown and the Message property contains information about what the exception is.

Snip: Exception Message for 4.3 Scratchpad Learning Activity (NEW). Use the adjacent task instructions to identify the required controls, output, or debugger values.
Snip: Exception Message
B Catch a NullReferenceException.

We don't have to catch all exceptions like the previous step. To give more specific messages we can catch individual exceptions.

1 Replace Exception with NullReferenceException.
2 Run the application and type in the command 'parking 10.50'. The exception should be caught successfully.
3 Type in 'parking ten'. This time the application will crash with a FormatException error because we never included a way to handle it.
Snip: FormatException for 4.3 Scratchpad Learning Activity (NEW). Use the adjacent task instructions to identify the required controls, output, or debugger values.
Snip: FormatException
C Catch all exceptions (again).
1 Remove the ex variable from the NullReferenceException catch; if you aren't using it you don't need it.
2 Under the first catch, add a second that catches all Exceptions and has an ex variable.
Snip: Exception ex for 4.3 Scratchpad Learning Activity (NEW). Use the adjacent task instructions to identify the required controls, output, or debugger values.
Snip: Exception ex
3 In the catch body, Console.WriteLine the Message of the Exception object.
Snip: ex.Message for 4.3 Scratchpad Learning Activity (NEW). Use the adjacent task instructions to identify the required controls, output, or debugger values.
Snip: ex.Message
4 Run the application and enter the commands 'new' and 'parking ten'. The console should write out the generic exception…

Run the application and enter the commands 'new' and 'parking ten'. The console should write out the generic exception message: 'Input was not in a correct format'.

D Catch a FormatException

We now know that there is a FormatException that could be thrown, but the message is too generic for users.

1 Add an additional catch statement after the first catch statement that catches a FormatException type.
Snip: Catch FormatExcpetion for 4.3 Scratchpad Learning Activity (NEW). Use the adjacent task instructions to identify the required controls, output, or debugger values.
Snip: Catch FormatExcpetion
2 In the catch body, Console.WriteLine a message with the text "Please enter a valid number for parking cost."
3 Start the application. Enter in the commands 'new' and 'parking ten'. You should see the message about correct number…

Start the application. Enter in the commands 'new' and 'parking ten'. You should see the message about correct number formatting that is more specific.

E Catch an IndexOutOfRangeException.
1 Run the application and enter the commands 'new' and 'parking'. You should get the exception message: 'Index was outside the…

Run the application and enter the commands 'new' and 'parking'. You should get the exception message: 'Index was outside the bounds of the array', which means our code is trying to get a value from an array that is too short.

2 Add another catch below the FormatException that catch an IndexOutOfRangeException.
3 In the catch body, Console.WriteLine a message with the text "Please enter a number to set the parking cost to."
4 Run the application and enter the commands 'new' and 'parking'. You should see the message about entering a number.
Throw an exception.
Acceptance Criteria
Acceptance criteria
  • The parking-cost property rejects values outside the stated allowed range by throwing the specified exception.
  • The console catches that exception and displays its message.
  • The 1000 test demonstrates the out-of-range response.
Instructions
6.Throw an exception.

We have a number of ways we can handle exceptions, but we want to throw an exception if a user inputs a value outside of a particular range for the ParkingCost. There's nothing that Visual Studio has that handles this specific situation so we need to throw an exception ourselves.

A In the ParkingCost setter in the the Airport class, test if the value is greater than or equal to 0 and less than or equal…

In the ParkingCost setter in the the Airport class, test if the value is greater than or equal to 0 and less than or equal to 100. If it is, assign the parkingCost to the value.

B In the else statement paste the code below:
throw new ArgumentOutOfRangeException();

If you've previously cleaned your usings, you will need to add a: using System;

Using the 'new' keyword here indicates that this exception is an object. We saw this before with NullReferenceExceptions and other types of exceptions.

An ArgumentOutOfRangeException is a type/class that inherits from Exception. You can F12 on it like any other class to explore what it is/does and what it inherits from.

C In the parenthesis of the ArgumentOutOfRangeException, add the string arguments "ParkingCost" and "Parking cost was out of range".
D Check Your Work:
1 Run the application and enter the commands 'new' and 'parking 1000'
2 The console should display as shown below:
Snip: Console Message for 4.3 Scratchpad Learning Activity (NEW). Use the adjacent task instructions to identify the required controls, output, or debugger values.
Snip: Console Message
Submit a zipped Visual Studio solution after completing the following:
7.Submit a zipped Visual Studio solution after completing the following:
A Ensure that the application has no compiler errors or warnings.
B Remove all bin and obj folders from the entire solution.
Completion review
8.Review the feature acceptance criteria
A Confirm the required results

Return to each story and verify its acceptance criteria. Preserve the submission format and destination stated in the activity; a reading, setup guide, lab, video demonstration, and oral final may require different evidence.

Add a new case for Parking Cost. Add error handling to the parking case. Allow the console to handle more than one word as a command. Create better messages for potential user mistakes. Throw an exception.