When you do that you are saying something bad

Info icon This preview shows pages 234–236. Sign up to view the full content.

View Full Document Right Arrow Icon
When you do that you are saying “something bad happened, but I don’t really know what it was.” That degree of uncertainty tends to imply that the app has lost control of its own internal consistency. It is a common practice to catch Exception in your top-level exception handlers (e.g., in Main or, as we’ll see later, in threading worker functions); and when you do, you normally need to terminate the application (or at least restart some subsystem). Of course, you might know perfectly well what might go wrong, and you’re catching Exception because you can’t be bothered to list the half dozen exception types you intend to handle in the same way. (Maybe F1 wasn’t working so well that day and you couldn’t inspect the docs; and someone was pressing you to check in your changes.) Beware! What happens if an implementation detail changes in another class on which you de- pend, such that a new exception is thrown? Your handler will swallow it up and carry on. That might be OK, and your game of tic-tac-toe will continue happily. Or it might have unintended consequences, such as data loss or the start of WWIII. You just can’t know in advance. When the flow of control leaves the try block successfully, or the flow of control exits the last catch block if an exception occurred in the try block, the code in the finally block is executed. In other words, the code in the finally block is always executed, regardless of whether there was an exception. If you designed your exception-handling code nicely, you’ll almost certainly use far more finally blocks than you do catch blocks. The finally block is a good place for cleaning up your resources, or winding back internal state if an error occurs, to ensure that your pre- and post conditions are still valid, whereas a catch block allows you to deal with an error condition you, as a client, understand in some way—even if it is only to present a message to the user (as in this case). If we compile and run our code again, we’ll see the following 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. Error running turtle: The PlatformWidth must be initialized to a value > 0.0 Waiting in the finally block... Notice how the error-handling code is now consolidated neatly into clearly defined blocks, rather than scattered throughout our code, and we’ve been able to cut down substantially on the number of points of return from our method. 210 | Chapter 6: Dealing with Errors
Image of page 234

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

View Full Document Right Arrow Icon
At the moment, we’re not handling any exceptions in our Turtle itself. Let’s imagine that our Turtle is being provided to clients in a library, and we (as the leading vendors of turtle simulators) want the library to do some internal logging when errors occur: maybe we have an opt-in customer experience program that sends telemetry back to our team.
Image of page 235
Image of page 236
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