Microsoft Visual C# 2010 Step by Step (P4)

Tham khảo sách 'microsoft visual c# 2010 step by step (p4)', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | 120 Part I Introducing Microsoft Visual C and Microsoft Visual Studio 2010 3. Type 9876543 in the left operand text box type 9876543 in the right operand text box click the Multiplication button 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 overflowed the 32-bit limit of the int type. 4. Click Quit and return to the Visual Studio 2010 programming environment. 5. In the Code and Text Editor window displaying locate the multiplyValues method. It looks like this private int multiplyValues int leftHandSide int rightHandSide return leftHandSide rightHandSide The return statement contains the multiplication operation that is silently overflowing. 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 OverflowException 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 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 click the Multiplication button and then click Calculate. The second catch handler successfully catches the OverflowException and displays the message Arithmetic operation resulted

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.