日期:2014-05-17  浏览次数:20770 次

新手提问,望大伙指点(关于添加捕捉异常块)
需要添加一个try块,捕捉异常,总是不能顺利添加,请指教哈
我添加红色字体代码,结果却不一样,请各位师兄帮忙看看问题出在哪,多谢!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace MathsOperators
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>

    public partial class Window1 : Window
    {

        public Window1()
        {
            InitializeComponent();
        }

        private void calculateClick(object sender, RoutedEventArgs e)
try
    {
            int leftHandSide = int.Parse(lhsOperand.Text);
            int rightHandSide = int.Parse(rhsOperand.Text);
            int answer = doCalculation(leftHandSide, rightHandSide);
            result.Text = answer.ToString();
    }
      catch (FormatException fEx)
{
    result.Text=fEx.Message;
}

        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);