Rightmotorstate motorstatereversed pi 2 seconds

Info icon This preview shows pages 231–235. Sign up to view the full content.

arthurTheTurtle.RightMotorState = MotorState.Reversed; // PI / 2 seconds should do the trick arthurTheTurtle.RunFor(Math.PI / 2.0); ShowPosition(arthurTheTurtle); Exceptions | 207
Image of page 231

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

// And let's go into reverse arthurTheTurtle.RightMotorState = MotorState.Reversed; arthurTheTurtle.LeftMotorState = MotorState.Reversed; // And run for 5 seconds arthurTheTurtle.RunFor(5); ShowPosition(arthurTheTurtle); // Then rotate back the other way arthurTheTurtle.RightMotorState = MotorState.Running; // And run for PI/4 seconds to give us 45 degrees arthurTheTurtle.RunFor(Math.PI / 4.0); ShowPosition(arthurTheTurtle); // And finally drive backwards for a bit arthurTheTurtle.RightMotorState = MotorState.Reversed; arthurTheTurtle.LeftMotorState = MotorState.Reversed; arthurTheTurtle.RunFor(Math.Cos(Math.PI / 4.0)); ShowPosition(arthurTheTurtle); } catch (InvalidOperationException e) { Console.WriteLine("Error running turtle:"); Console.WriteLine(e.Message); } finally { Console.ReadKey(); } } If any of the code in the try block throws an exception, the runtime looks to see if there are any catch blocks whose exception type matches the type of that exception. It matches successfully if the catch parameter is either of the same type, or of a less-derived (base) type than that of the exception. You can have any number of catch blocks for different types of exceptions, and it will look through them in the order they are defined; the first one that matches wins (even if there is a “better” match farther down). If it doesn’t find a suitable match, the exception will be propagated on up the call stack, just as though there was no try block. To see how this works in practice, let’s quickly modify the code in Example 6-14 to catch Exception as well, as shown in Example 6-15 . 208 | Chapter 6: Dealing with Errors
Image of page 232
Example 6-15. Poorly placed catch block try { ... } catch (Exception e2) { Console.WriteLine("Caught generic exception..."); } catch (InvalidOperationException e) { Console.WriteLine("Error running turtle:"); Console.WriteLine(e.Message); } finally { Console.WriteLine("Waiting in the finally block..."); Console.ReadKey(); } If you try to compile this, you’ll see the following error: A previous catch clause already catches all exceptions of this or of a super type ('System.Exception') This occurs because Exception is an ancestor of InvalidOperationException , and the clause appears first in the list of catch blocks. If we switch those around, we compile successfully, as shown in Example 6-16 . Example 6-16. Catching exceptions in the right order try { ... } catch (InvalidOperationException e) { Console.WriteLine("Error running turtle:"); Console.WriteLine(e.Message); } catch (Exception e2) { Console.WriteLine("Caught generic exception..."); } finally { Console.WriteLine("Waiting in the finally block..."); Console.ReadKey(); } Exceptions | 209
Image of page 233

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

Catching Too Much You should consider very carefully whether you want to catch instances of the base Exception type.
Image of page 234
Image of page 235
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