Timeoutexception contended resource a method tried to

Info icon This preview shows pages 240–243. Sign up to view the full content.

View Full Document Right Arrow Icon
TimeoutException Contended resource A method tried to access a scarce resource of some kind (memory or a hardware device that cannot be shared) and it was not available because someone else was using it. OutOfMemoryException TimeoutException Obviously, that’s a much abbreviated list, but it contains some of the most common exceptions you’ll see in real applications. One of the most useful that you’ll throw yourself is the ArgumentException . You can use that when parameters passed to your methods fail to validate. Let’s make use of that in our RunFor method. Say that a “feature” of our turtle hardware is that it crashes and becomes unresponsive if we try to run it for zero seconds. We can work around this in our software by checking for this condition in the RunFor method, and throwing an exception if clients try this, as shown in Example 6-21 . Example 6-21. Throwing an exception when arguments are bad public void RunFor(double duration) { if (duration <= double.Epsilon) { throw new ArgumentException( "Must provide a duration greater than 0", "duration"); } try { // ... } catch (InvalidOperationException iox) 216 | Chapter 6: Dealing with Errors
Image of page 240

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

View Full Document Right Arrow Icon
{ throw new Exception("Some problem with the turtle has occurred", iox); } catch (Exception ex) { // Log here Console.WriteLine("Log error: " + ex.Message); // Rethrow throw; } finally { Console.WriteLine("In the Turtle finally block"); } } The second parameter in this constructor should match the name of the parameter that is in error. The first represents the exception message. When you come to use ArgumentNullException (which you throw when you are erroneously passed a null argument) you’ll find that the error message and parameter arguments are swapped around in the construc- tor. This irritating inconsistency has been with us since .NET 1.0, and too much code depends on it to fix it now. The code in Example 6-22 updates Main , to sneak in an attempt to run it for zero seconds. Example 6-22. Testing for the expected exception static void Main(string[] args) { Turtle arthurTheTurtle = new Turtle { PlatformWidth = 0.0, PlatformHeight = 10.0, MotorSpeed = 5.0 }; ShowPosition(arthurTheTurtle); try { arthurTheTurtle.RunFor(0.0); // ... } 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) Exceptions | 217
Image of page 241
{ Console.WriteLine(current.Message); current = current.InnerException; } } finally { Console.WriteLine("Waiting in the finally block"); Console.ReadKey(); } } If we compile and run, we’ll see the following output: Arthur is at (0,0) and is pointing at angle 0.00 radians. Must provide a duration greater than 0 Parameter name: duration Waiting in the finally block Notice how the error message automatically includes the details of the problem parameter.
Image of page 242

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

View Full Document Right Arrow Icon
Image of page 243
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