Things would be even worse if this was in a library

Info icon This preview shows pages 222–225. Sign up to view the full content.

View Full Document Right Arrow Icon
Things would be even worse if this was in a library; we could recompile against this new version, and everything would seem fine, while in the background everything would quietly be going horribly wrong. It is probably about time we did something with the error message, so let’s see what happens in our client code (see Example 6-10 ). Example 6-10. Handling an error static void Main(string[] args) { Turtle arthurTheTurtle = new Turtle { PlatformWidth = 0.0, PlatformHeight = 10.0, MotorSpeed = 5.0 }; ShowPosition(arthurTheTurtle); // We want to proceed forwards arthurTheTurtle.LeftMotorState = MotorState.Running; arthurTheTurtle.RightMotorState = MotorState.Running; // For two seconds TurtleError result = arthurTheTurtle.RunFor(2.0); if (result != TurtleError.OK) { HandleError(result); return; } ShowPosition(arthurTheTurtle); 198 | Chapter 6: Dealing with Errors
Image of page 222

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
// Now, let's rotate clockwise for a bit arthurTheTurtle.RightMotorState = MotorState.Reversed; // PI / 2 seconds should do the trick result = arthurTheTurtle.RunFor(Math.PI / 2.0); if (result != TurtleError.OK) { HandleError(result); return; } ShowPosition(arthurTheTurtle); // And let's go into reverse arthurTheTurtle.RightMotorState = MotorState.Reversed; arthurTheTurtle.LeftMotorState = MotorState.Reversed; // And run for 5 seconds result = arthurTheTurtle.RunFor(5); if (result != TurtleError.OK) { HandleError(result); return; } ShowPosition(arthurTheTurtle); // Then rotate back the other way arthurTheTurtle.RightMotorState = MotorState.Running; // And run for PI/4 seconds to give us 45 degrees result = arthurTheTurtle.RunFor(Math.PI / 4.0); if (result != TurtleError.OK) { HandleError(result); return; } ShowPosition(arthurTheTurtle); // And finally drive backwards for a bit arthurTheTurtle.RightMotorState = MotorState.Reversed; arthurTheTurtle.LeftMotorState = MotorState.Reversed; result = arthurTheTurtle.RunFor(Math.Cos(Math.PI / 4.0)); if (result != TurtleError.OK) { HandleError(result); return; } ShowPosition(arthurTheTurtle); Console.ReadKey(); } private static void HandleError(TurtleError result) Returning Error Values | 199
Image of page 223
{ Console.WriteLine("We hit turtle error {0}", result); Console.ReadKey(); } Every time we call the RunFor method, we have to stash away the error message that is returned, check it for problems, and then decide what we’re going to do. In this instance, we decided to quit the application, after showing an error message to the user, because it isn’t safe to continue. If we compile and run, here’s the output: Arthur is at (0,0) and is pointing at angle 0.00 radians. Arthur is at (0,10) and is pointing at angle 0.00 radians. We hit turtle error RotateError From an application point of view, this is much better behavior than before: we were able to stop when we hit our first problem. Unfortunately, we had to write quite a lot of boilerplate code to achieve that end, and our code is much less readable than it was before. We also created a huge number of potential exit points out of our function, which decreases its maintainability. So while it is better, I’m not totally happy with it; this is catching just one potential error from one function, and we have almost as many
Image of page 224

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 225
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern