microsoft visual c 2008 step by step phần 3 pdf

67 497 0
microsoft visual c 2008 step by step phần 3 pdf

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

108 Part I Introducing Microsoft Visual C# and Microsoft Visual Studio 2008 FormatException and Exception in two different handlers, which one will run (or will both execute)? When an exception occurs, the fi rst handler found by the runtime that matches the excep- tion is used, and the others are ignored. What this means is that if you place a handler for Exception before a handler for FormatException, the FormatException handler will never run. Therefore, you should place more-specifi c catch handlers above a general catch handler after a try block. If none of the specifi c catch handlers matches the exception, the general catch handler will. In the following exercise, you will write a try block and catch an exception. Write a statement 1. Start Visual Studio 2008 if it is not already running. 2. Open the MathsOperators solution located in the \Microsoft Press\Visual CSharp Step By Step\Chapter 6\MathsOperators folder in your Documents folder. This is a variation on the program that you fi rst saw in Chapter 2, “Working with Variables, Operators, and Expressions.” It was used to demonstrate the different arithmetic operators. 3. On the Debug menu, click Start Without Debugging. The form appears. You are now going to enter some text that is deliberately not valid in the left operand text box. This operation will demonstrate the lack of robustness in the current version of the program. 4. Type John in the left operand text box, and then click Calculate. A dialog box reports an unhandled exception; the text you entered in the left operand text box caused the application to fail. Note The Debug button does not appear if you are using Microsoft Visual C# 2008 Express Edition. You might see a different version of this dialog box (shown later) depending on how you have confi gured problem reporting in Control Panel. If you see this dialog box, simply click the Close the program link whenever the instructions in the following steps refer to the Close Program button, and click the Debug the program link whenever W r ite a state m e n t Chapter 6 Managing Errors and Exceptions 109 the instructions refer to the Debug button. (If you are using Windows XP rather than Windows Vista, you will see a different dialog box with Debug, Send Error Report, and Don’t Send buttons. Click the Don’t Send button to close the program.) 5. If you are using Visual Studio 2008, click Debug. In the Visual Studio Just-In-Time Debugger dialog box, in the Possible Debuggers list box, select MathsOperators – Microsoft Visual Studio: Visual Studio 2008, and then click Yes: 6. If you are using Visual C# 2008 Express Edition, click Close Program. On the Debug menu, click Start Debugging. Type John in the left operand text box, and then click Calculate. 7. Whether you are using Visual Studio 2008 or Visual C# 2008 Express Edition, the Visual Studio 2008 debugger starts and highlights the line of code that caused the exception and displays some additional information about the exception: 110 Part I Introducing Microsoft Visual C# and Microsoft Visual Studio 2008 You can see that the exception was thrown by the call to int.Parse inside the calculateClick method. The problem is that this method is unable to parse the text “John” into a valid number. Note You can view the code that caused an exception only if you actually have the source code available on your computer. 8. On the Debug menu, click Stop Debugging. 9. Display the code for the fi le Window1.xaml.cs in the Code and Text Editor window, and locate the calculateClick method. 10. Add a try block (including braces) around the four statements inside this method, as shown in bold type here: try { int leftHandSide = int.Parse(lhsOperand.Text); int rightHandSide = int.Parse(rhsOperand.Text); int answer = doCalculation(leftHandSide, rightHandSide); result.Text = answer.ToString(); } 11. Add a catch block immediately after the closing brace for this new try block, as follows: catch (FormatException fEx) { Chapter 6 Managing Errors and Exceptions 111 result.Text = fEx.Message; } This catch handler catches the FormatException thrown by int.Parse and then displays in the result text box at the bottom of the form the text in the exception’s Message property. 12. On the Debug menu, click Start Without Debugging. 13. Type John in the left operand text box, and then click Calculate. The catch handler successfully catches the FormatException, and the message “Input string was not in a correct format” is written to the Result text box. The application is now a bit more robust. 14. Replace John with the number 10, type Sharp in the right operand text box, and then click Calculate. Notice that because the try block surrounds the statements that parse both text boxes, the same exception handler handles user input errors in both text boxes. 15. Click Quit to return to the Visual Studio 2008 programming environment. Using Checked and Unchecked Integer Arithmetic In Chapter 2, you learned how to use binary arithmetic operators such as + and * on primi- tive data types such as int and double. You also saw that the primitive data types have a fi xed size. For example, a C# int is 32 bits. Because int has a fi xed size, you know exactly the range of value that it can hold: it is –2147483648 to 2147483647. Tip If you want to refer to the minimum or maximum value of int in code, you can use the int. MinValue or int.MaxValue property. 112 Part I Introducing Microsoft Visual C# and Microsoft Visual Studio 2008 The fi xed size of the int type creates a problem. For example, what happens if you add 1 to an int whose value is currently 2147483647? The answer is that it depends on how the ap- plication is compiled. By default, the C# compiler generates code that allows the calculation to overfl ow silently. In other words, you get the wrong answer. (In fact, the calculation wraps around to the largest negative integer value, and the result generated is –2147483648.) The reason for this behavior is performance: integer arithmetic is a common operation in almost every program, and adding the overhead of overfl ow checking to each integer expression could lead to very poor performance. In many cases, the risk is acceptable because you know (or hope!) that your int values won’t reach their limits. If you don’t like this approach, you can turn on overfl ow checking. Tip You can activate and disable overfl ow checking in Visual Studio 2008 by setting the project properties. On the Project menu, click YourProject Properties (where YourProject is the name of your project). In the project properties dialog box, click the Build tab. Click the Advanced button in the lower-right corner of the page. In the Advanced Build Settings dialog box, select or clear the Check for arithmetic overfl ow/underfl ow check box. Regardless of how you compile an application, you can use the checked and unchecked keywords to turn on and off integer arithmetic overfl ow checking selectively in parts of an application that you think need it. These keywords override the compiler option specifi ed for the project. Writing Checked Statements A checked statement is a block preceded by the checked keyword. All integer arithmetic in a checked statement always throws an Overfl owException if an integer calculation in the block overfl ows, as shown in this example: int number = int.MaxValue; checked { int willThrow = number++; Console.WriteLine(“this won’t be reached”); } Important Only integer arithmetic directly inside the checked block is subject to overfl ow checking. For example, if one of the checked statements is a method call, checking does not apply to code that runs in the method that is called. Chapter 6 Managing Errors and Exceptions 113 You can also use the unchecked keyword to create an unchecked block statement. All integer arithmetic in an unchecked block is not checked and never throws an Overfl owException. For example: int number = int.MaxValue; unchecked { int wontThrow = number++; Console.WriteLine(“this will be reached”); } Writing Checked Expressions You can also use the checked and unchecked keywords to control overfl ow checking on inte- ger expressions by preceding just the individual parenthesized expression with the checked or unchecked keyword, as shown in this example: int wontThrow = unchecked(int.MaxValue + 1); int willThrow = checked(int.MaxValue + 1); The compound operators (such as += and -=) and the increment (++) and decrement ( ) operators are arithmetic operators and can be controlled by using the checked and un- checked keywords. Remember, x += y; is the same as x = x + y;. Important You cannot use the checked and unchecked keywords to control fl oating-point (non- integer) arithmetic. The checked and unchecked keywords apply only to integer arithmetic using data types such as int and long. Floating-point arithmetic never throws Overfl owException—not even when you divide by 0.0. (The .NET Framework has a representation for infi nity.) In the following exercise, you will see how to perform checked arithmetic when using Visual Studio 2008. Use checked expressions 1. Return to Visual Studio 2008. 2. On the Debug menu, click Start Without Debugging. You will now attempt to multiply two large values. 3. Type 9876543 in the left operand text box, type 9876543 in the right operand text box, select the Multiplication option, and then click Calculate. The value –1195595903 appears in the Result text box on the form. This is a negative value, which cannot possibly be correct. This value is the result of a multiplication operation that silently overfl owed the 32-bit limit of the int type. U se checked ex p ress i ons 114 Part I Introducing Microsoft Visual C# and Microsoft Visual Studio 2008 4. Click Quit, and return to the Visual Studio 2008 programming environment. 5. In the Code and Text Editor window displaying Window1.xaml.cs, locate the multiplyValues method. It looks like this: private int multiplyValues(int leftHandSide, int rightHandSide) { expression.Text = leftHandSide.ToString() + “ * “ + rightHandSide.ToString(); return leftHandSide * rightHandSide; } The return statement contains the multiplication operation that is silently overfl owing. 6. Edit the return statement so that the return value is checked, like this: return checked(leftHandSide * rightHandSide); The multiplication is now checked and will throw an Overfl owException rather than silently returning the wrong answer. 7. Locate the calculateClick method. 8. Add the following catch handler immediately after the existing FormatException catch handler in the calculateClick method: catch (OverflowException oEx) { result.Text = oEx.Message; } Tip The logic of this catch handler is the same as that for the FormatException catch handler. However, it is still worth keeping these handlers separate rather than simply writing a generic Exception catch handler because you might decide to handle these exceptions differently in the future. 9. On the Debug menu, click Start Without Debugging to build and run the application. 10. Type 9876543 in the left operand text box, type 9876543 in the right operand text box, select the Multiplication option, and then click Calculate. The second catch handler successfully catches the Overfl owException and displays the message “Arithmetic operation resulted in an overfl ow” in the Result text box. 11. Click Quit to return to the Visual Studio 2008 programming environment. Throwing Exceptions Suppose you are implementing a method called monthName that accepts a single int argument and returns the name of the corresponding month. For example, monthName(1) returns “January”, monthName(2) returns “February”, and so on. The question is: What should Chapter 6 Managing Errors and Exceptions 115 the method return when the integer argument is less than 1 or greater than 12? The best answer is that the method shouldn’t return anything at all; it should throw an exception. The .NET Framework class libraries contain lots of exception classes specifi cally designed for situations such as this. Most of the time, you will fi nd that one of these classes describes your exceptional condition. (If not, you can easily create your own exception class, but you need to know a bit more about the C# language before you can do that.) In this case, the existing .NET Framework ArgumentOutOfRangeException class is just right. You can throw an exception by using the throw statement, as shown in the following example: public static string monthName(int month) { switch (month) { case 1 : return “January”; case 2 : return “February”; case 12 : return “December”; default : throw new ArgumentOutOfRangeException(“Bad month”); } } The throw statement needs an exception object to throw. This object contains the details of the exception, including any error messages. This example uses an expression that creates a new ArgumentOutOfRangeException object. The object is initialized with a string that will populate its Message property by using a constructor. Constructors are covered in detail in Chapter 7, “Creating and Managing Classes and Objects.” In the following exercises, you will add to the MathsOperators project code for throwing an exception. Throw your own exception 1. Return to Visual Studio 2008. 2. On the Debug menu, click Start Without Debugging. 3. Type 24 in the left operand text box, type 36 in the right operand text box, and then click Calculate. The value 0 appears in the Result text box. The fact that you have not selected an operator option is not immediately obvious. It would be useful to write a diagnostic message in the Result text box. 4. Click Quit to return to the Visual Studio 2008 programming environment. T hrow your own exceptio n 116 Part I Introducing Microsoft Visual C# and Microsoft Visual Studio 2008 5. In the Code and Text Editor window displaying Window1.xaml.cs, locate and examine the doCalculation method. It looks like this: private int doCalculation(int leftHandSide, int rightHandSide) { int result = 0; if (addition.IsChecked.HasValue && addition.IsChecked.Value) result = addValues(leftHandSide, rightHandSide); else if (subtraction.IsChecked.HasValue && subtraction.IsChecked.Value) result = subtractValues(leftHandSide, rightHandSide); else if (multiplication.IsChecked.HasValue && multiplication.IsChecked.Value) result = multiplyValues(leftHandSide, rightHandSide); else if (division.IsChecked.HasValue && division.IsChecked.Value) result = divideValues(leftHandSide, rightHandSide); else if (remainder.IsChecked.HasValue && remainder.IsChecked.Value) result = remainderValues(leftHandSide, rightHandSide); return result; } The addition, subtraction, multiplication, division, and remainder fi elds are the radio buttons that appear on the form. Each radio button has a property called IsChecked that indicates whether the user has selected it. The IsChecked property is an example of a nullable value, which means it can either contain a specifi c value or be in an unde- fi ned state. (You learn more about nullable values in Chapter 8, “Understanding Values and References.”) The IsChecked.HasValue property indicates whether the radio button is in a defi ned state, and if it is, the IsChecked.Value property indicates what this state is. The IsChecked.Value property is a Boolean that has the value true if the radio button is selected or false otherwise. The cascading if statement examines each radio button in turn to fi nd which one is selected. (The radio buttons are mutually exclusive, so the user can select only one radio button at most.) If none of the buttons is selected, none of the if statements will be true and the result variable will remain at its initial value (0). This variable holds the value that is returned by the method. You could try to solve the problem by adding one more else statement to the if-else cascade to write a message to the result text box on the form. However, this solution is not a good idea because it is not really the purpose of this method to output messages. It is better to separate the detection and signaling of an error from the catching and handling of that error. 6. Add another else statement to the list of if-else statements (immediately before the return statement), and throw an InvalidOperationException exactly as follows: else throw new InvalidOperationException(“no operator selected”); 7. On the Debug menu, click Start Without Debugging to build and run the application. 8. Type 24 in the left operand text box, type 36 in the right operand text box, and then click Calculate. Chapter 6 Managing Errors and Exceptions 117 An exception dialog box appears. The application has thrown an exception, but your code does not catch it yet. 9. Click Close program. The application terminates, and you return to Visual Studio 2008. Now that you have written a throw statement and verifi ed that it throws an exception, you will write a catch handler to handle this exception. Catch your own exception 1. In the Code and Text Editor window displaying Window1.xaml.cs, locate the calculateClick method. 2. Add the following catch handler immediately below the existing two catch handlers in the calculateClick method: catch (InvalidOperationException ioEx) { result.Text = ioEx.Message; } This code catches the InvalidOperationException that is thrown when no operator radio button is selected. 3. On the Debug menu, click Start Without Debugging. 4. Type 24 in the left operand text box, type 36 in the right operand text box, and then click Calculate. The message “no operator selected” appears in the Result text box. 5. Click Quit. The application is now a lot more robust than it was. However, several exceptions could still arise that would not be caught and that might cause the application to fail. For example, if you attempt to divide by 0, an unhandled DivideByZeroException will be thrown. (Integer divi- sion by 0 does throw an exception, unlike fl oating-point division by 0.) One way to solve this is to write an ever larger number of catch handlers inside the calculateClick method. However, a better solution is to add a general catch handler that catches Exception at the end of the list of catch handlers. This will trap all unhandled exceptions. Tip The decision of whether to catch all unhandled exceptions explicitly in a method depends on the nature of the application you are building. In some cases, it makes sense to catch exceptions as close as possible to the point at which they occur. In other situations, it is more useful to let an exception propagate back to the method that invoked the routine that threw the exception. C atch your own exceptio n [...]... is incremented by the Circle constructor every time a new Circle object is created: class Circle { public Circle() // default constructor { radius = 0; NumCircles++; } public Circle(int initialRadius) // overloaded constructor { radius = initialRadius; NumCircles++; } private int radius; public static int NumCircles = 0; } All Circle objects share the same NumCircles field, so the statement NumCircles++;... parts of the class by using the partial keyword in each file For example, if the Circle class is split between two files called circ1.cs (containing the constructors) and circ2.cs (containing the methods and fields), the contents of circ1.cs look like this: partial class Circle { public Circle() // default constructor { radius = 0; } public Circle(int initialRadius) // overloaded constructor { radius = initialRadius;... NumCircles++; increments the same data every time a new instance is created You access the NumCircles field by specifying the Circle class rather than a Circle object For example: Console.WriteLine(“Number of Circle objects: {0}”, Circle.NumCircles); Tip static methods are also called class methods However, static fields aren’t usually called class fields; they’re just called static fields (or sometimes static variables)... Part I Introducing Microsoft Visual C# and Microsoft Visual Studio 2008 Catch unhandled exceptions 1 In the Code and Text Editor window displaying Window1.xaml.cs, locate the calculateClick method 2 Add the following catch handler to the end of the list of existing catch handlers: catch (Exception ex) { result.Text = ex.Message; } This catch handler will catch all hitherto unhandled exceptions, whatever... scope of the current discussion You can now use the Circle class and exercise its Area method Notice how you use dot notation to invoke the Area method on a Circle object: Circle c; c = new Circle(); double areaOfCircle = c. Area(); Overloading Constructors You’re almost finished, but not quite You can now declare a Circle variable, point it to a newly created Circle object, and then call its Area method... data items Control the accessibility of members by using the public and private keywords Create objects by using the new keyword to invoke a constructor Write and call your own constructors Create methods and data that can be shared by all instances of the same class by using the static keyword In Part I, “Introducing Microsoft Visual C# and Microsoft Visual Studio 2008, ” you learned how to declare variables,... to increment the objectCount field, as shown in bold type in the following code example Each time an object is created, its constructor is called As long as you increment the objectCount in each constructor (including the default constructor), objectCount will hold the number of objects created so far This strategy works only because objectCount is a shared static field If objectCount were an instance... using Visual Studio 2008) or Save (if you are using Visual C# 2008 Express Edition) and save the project Chapter 6 Quick Reference To Do this Throw an exception Use a throw statement For example: throw new FormatException(source); Ensure that integer arithmetic is always checked for overflow Use the checked keyword For example: Catch a speci c exception Write a catch handler that catches the speci c exception... class occur in the body of the class between a pair of braces Here is a C# class called Circle that contains one method (to calculate the circle’s area) and one piece of data (the circle’s radius): class Circle { double Area() { return Math.PI * radius * radius; } int radius; } Chapter 7 Creating and Managing Classes and Objects 125 Note The Math class contains methods for performing mathematical calculations... and y-coordinates of a point and will provide constructors for initializing these fields You will create instances of the class by using the new keyword and calling the constructors Write constructors and create objects 1 Start Visual Studio 2008 if it is not already running 2 Open the Classes project located in the \Microsoft Press \Visual CSharp Step by Step\ Chapter 7\Classes folder in your Documents . the exception: 110 Part I Introducing Microsoft Visual C# and Microsoft Visual Studio 2008 You can see that the exception was thrown by the call to int.Parse inside the calculateClick method arithmetic is always checked for overfl ow Use the checked keyword. For example: int number = Int32.MaxValue; checked { number++; } Catch a specifi c exception Write a catch handler that catches. catches the specifi c exception class. For example: try { } catch (FormatException fEx) { } Catch all exceptions in a single catch handler Write a catch handler that catches Exception. For

Ngày đăng: 12/08/2014, 21:20

Từ khóa liên quan

Mục lục

  • Part I: Introducing Microsoft Visual C# and Microsoft Visual Studio 2008

    • Chapter 6: Managing Errors and Exceptions

      • Using Checked and Unchecked Integer Arithmetic

        • Writing Checked Statements

        • Writing Checked Expressions

        • Throwing Exceptions

        • Using a finally Block

        • Chapter 6 Quick Reference

        • Part II: Understanding the C# Language

          • Chapter 7: Creating and Managing Classes and Objects

            • Understanding Classification

            • The Purpose of Encapsulation

            • Defining and Using a Class

            • Controlling Accessibility

            • Working with Constructors

              • Overloading Constructors

              • Understanding static Methods and Data

                • Creating a Shared Field

                • Creating a static Field by Using the const Keyword

                • Chapter 7 Quick Reference

                • Chapter 8: Understanding Values and References

                  • Copying Value Type Variables and Classes

                  • Understanding Null Values and Nullable Types

                    • Using Nullable Types

                    • Understanding the Properties of Nullable Types

                    • Using ref and out Parameters

                      • Creating ref Parameters

                      • Creating out Parameters

                      • How Computer Memory Is Organized

                        • Using the Stack and the Heap

                        • The System.Object Class

Tài liệu cùng người dùng

Tài liệu liên quan