Running rotateduration catch iox throw new

Info icon This preview shows pages 237–239. Sign up to view the full content.

View Full Document Right Arrow Icon
RightMotorState == MotorState.Running)) { Rotate(duration); } } catch (InvalidOperationException iox) { throw new Exception("Some problem with the turtle..." , iox ); } catch (Exception ex) { // Log here Console.WriteLine("Log message: " + ex.Message); // Rethrow throw; } } Notice how we passed the exception to be wrapped as a parameter to the new exception when we constructed it. Let’s make a quick modification to the exception handler in Main to take advantage of this new feature (see Example 6-19 ). Example 6-19. Reporting an InnerException static void Main(string[] args) { Turtle arthurTheTurtle = new Turtle { PlatformWidth = 0.0, PlatformHeight = 10.0, MotorSpeed = 5.0 }; ShowPosition(arthurTheTurtle); try { // ... } catch (InvalidOperationException e) { Console.WriteLine("Error running turtle:"); Console.WriteLine(e.Message); } catch (Exception e1) { // Loop through the inner exceptions, printing their messages Exception current = e1; while (current != null) { Console.WriteLine(current.Message); current = current.InnerException; } } finally Exceptions | 213
Image of page 237

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

View Full Document Right Arrow Icon
{ Console.WriteLine("Waiting in the finally block"); Console.ReadKey(); } } If we compile and run again, we can see the following output, including the messages from both the outer and inner exceptions: 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. Some problem with the turtle has occurred The PlatformWidth must be initialized to a value > 0.0 Waiting in the finally block Clearly, wrapping an implementation-detail exception with something explicitly docu- mented in our public contract can simplify the range of exception handlers you require. It also helps to encapsulate implementation details, as the exceptions you throw can be considered part of your contract. On the other hand, are there any disadvantages to throwing a wrapped exception (or indeed rethrowing the original exception explicitly, rather than implicitly with throw; )? As programming tends to be a series of compromises, the answer is, as you might expect, yes. If you explicitly (re)throw an exception, the call stack in the exception handler starts at the new throw statement, losing the original context in the debugger (although you can still inspect it in the inner exception in the object browser). This makes debugging noticeably less productive. Because of this, you should consider carefully whether you need to wrap the exception, and always ensure that you implicitly (rather than explicitly) rethrow exceptions that you have caught and then wish to pass through. When Do finally Blocks Run? It is worth clarifying exactly when the finally block gets executed, under a few edge conditions. First, let’s see what happens if we run our example application outside the debugger. If we do that (by pressing Ctrl-F5) we’ll see that Windows Error Handling * materializes, and presents the user with an error dialog before we actually hit our finally block at all! It is like the runtime has inserted an extra catch block in our own (top-level) ex- ception handler, rather than percolating up another level (and hence out of our scope, invoking the code in the finally block).
Image of page 238
Image of page 239
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