C#輸入類型檢查異常

 public partial class MainWindow : Window
    {

        public MainWindow()
        {
            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;
            }
            catch (OverflowException oEx)//數據溢出
            {
                result.Text = oEx.Message;
            }
            catch (InvalidOperationException ioEx)//非法操作異常
            {
                result.Text = ioEx.Message;
            }
            catch (Exception ex)
            {
                result.Text = ex.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);
            else if (remainder.IsChecked.HasValue && remainder.IsChecked.Value)
                result = remainderValues(leftHandSide, rightHandSide);
            else
                throw new InvalidOperationException("No operator selected");

            return result;
        }

        private int addValues(int leftHandSide, int rightHandSide)
        {
            expression.Text = leftHandSide.ToString() + " + " + rightHandSide.ToString();
            return leftHandSide + rightHandSide;
        }

        private int subtractValues(int leftHandSide, int rightHandSide)
        {
            expression.Text = leftHandSide.ToString() + " - " + rightHandSide.ToString();
            return leftHandSide - rightHandSide;
        }

        private int multiplyValues(int leftHandSide, int rightHandSide)
        {
            expression.Text = leftHandSide.ToString() + " * " + rightHandSide.ToString();
            return checked(leftHandSide * rightHandSide);
        }

        private int divideValues(int leftHandSide, int rightHandSide)
        {
            expression.Text = leftHandSide.ToString() + " / " + rightHandSide.ToString();
            return leftHandSide / rightHandSide;
        }

        private int remainderValues(int leftHandSide, int rightHandSide)
        {
            expression.Text = leftHandSide.ToString() + " % " + rightHandSide.ToString();
            return leftHandSide % rightHandSide;
        }

        private void quitClick(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }

發佈了52 篇原創文章 · 獲贊 18 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章