Lines of code dealing with that as we do our success

Info icon This preview shows pages 224–227. Sign up to view the full content.

lines of code dealing with that as we do our success scenario, scattered throughout our whole program! Debugging with Return Values So we finally spotted the problem, and stopped it from causing trouble. How do we find out what is wrong? Well, first we should take a look at the error message. That tells us that it has something to do with rotating the turtle, which gives us a bit of a clue. The easiest way to see what is really going on, though, might be to set a breakpoint in our error handler and see what state the system is in when the error occurs. To set a breakpoint, we can put the cursor on the line where we want to break into the debugger, and press F9. Figure 6-1 shows the code with a breakpoint set. Figure 6-1. Code with a breakpoint set If we run this now, the application will break into the debugger when we hit our error handler. If we press Ctrl-Alt-C, we can inspect the call stack to see where we went wrong, as shown in Figure 6-2 . 200 | Chapter 6: Dealing with Errors
Image of page 224

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

As you can see, there’s not an awful lot to help us; we lost context in which the error occurred because we returned out of the method that had the actual problem, and wound back up to our calling function. It isn’t completely useless—we now know which call had the problem (this time), so we can put a breakpoint on the relevant line and run again; but what if this was a hard- to-reproduce, intermittent error? We may have lost our one chance this week to identify and fix the problem! These are not the only problems with a return-value-based approach to error handling. What if we already need to use the return value on the method? We’re heading into the realm of “magic” values that mean an error has occurred, or we could add out or ref parameters to allow our method to return both a useful output and an error code. And what about property setters; we don’t have the option of a return value, but we might well like to return an error of some kind if the value is out of range. If you’re thinking “surely there has to be a better way,” you’re right. C# (like most modern languages) supports an alternative means of signaling errors: exceptions . Exceptions Rather than return an error code from a method, we can instead throw an instance of any type derived from Exception . Let’s rewrite our Rotate method to do that (see Ex- ample 6-11 ). Example 6-11. Indicating an error with an exception private void Rotate(double duration) { if (PlatformWidth <= 0.0) { throw new InvalidOperationException( "The PlatformWidth must be initialized to a value > 0.0"); } // This is the total circumference of turning circle double circum = Math.PI * PlatformWidth; // This is the total distance traveled double d = duration * MotorSpeed; if (LeftMotorState == MotorState.Reversed) Figure 6-2. Call stack, broken in the error handler Exceptions | 201
Image of page 225
{ // And we're going backwards if the motors are reversed d *= −1.0; } // So we've driven it this proportion of the way round double proportionOfWholeCircle = d / circum; // Once round is 360 degrees (or 2pi radians), so we have traveled CurrentOrientation = CurrentOrientation + (Math.PI * 2.0 * proportionOfWholeCircle);
Image of page 226

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

Image of page 227
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '15

{[ 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