在计算器中何处使用异常处理?

时间:2015-11-19 08:21:14

标签: c# exception-handling

我创建了计算器。它最终运行但是一旦我开始输入任何不正确的东西它会抛出FormatException。我在下面附上我的代码,请建议我如何在我的程序中处理异常。

MyCalculator.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyCalculator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        float number, answer;
        int count;

        public void disable() //create new method to disable calculator
        {
            //follow are disable when call we disable() function
            Display.Enabled = false;
            On.Show();//it will be still display
            offbutton.Hide(); //it will be hide
            back.Enabled = false;
            clear.Enabled = false;
            Add.Enabled = false;
            Sub.Enabled = false;
            Multiply.Enabled = false;
            Divide.Enabled = false;
            Zero.Enabled = false;
            One.Enabled = false;
            Two.Enabled = false;
            Three.Enabled = false;
            Four.Enabled = false;
            Five.Enabled = false;
            Six.Enabled = false;
            Seven.Enabled = false;
            Eight.Enabled = false;
            Nine.Enabled = false;
            equal.Enabled = false;
            Point.Enabled = false;
        }

        public void enable() //create new method to enable calculator
        {
            //follow are enable we call enable() function
            Display.Enabled = true;
            On.Hide();//it will be hide
            offbutton.Show();//it will be still display 
            back.Enabled = true;
            clear.Enabled = true;
            Add.Enabled = true;
            Sub.Enabled = true;
            Multiply.Enabled = true;
            Divide.Enabled = true;
            Zero.Enabled = true;
            One.Enabled = true;
            Two.Enabled = true;
            Three.Enabled = true;
            Four.Enabled = true;
            Five.Enabled = true;
            Six.Enabled = true;
            Seven.Enabled = true;
            Eight.Enabled = true;
            Nine.Enabled = true;
            equal.Enabled = true;
            Point.Enabled = true;
        }
        private void Point_Click(object sender, EventArgs e)
        {
         //Display dot(.) in textbox when press dot(.) button with red color
            Display.Text = Display.Text+".";
            Display.ForeColor = Color.Red;
        }

        private void Zero_Click(object sender, EventArgs e)
        {
       //Display zero(0) in textbox when press zero(0) button with red color
            Display.Text = Display.Text + 0;
            Display.ForeColor = Color.Red;
        }

        private void One_Click(object sender, EventArgs e)
        {
            //Display 1 in textboc when press 1 button with red color
            Display.Text = Display.Text + 1;
            Display.ForeColor = Color.Red;
        }

        private void Two_Click(object sender, EventArgs e)
        {
            //Display 2 in textboc when press 2 button with red color
            Display.Text = Display.Text + 2;
            Display.ForeColor = Color.Red;
        }

        private void Three_Click(object sender, EventArgs e)
        {
            //Display 3 in textboc when press 3 button with red color
            Display.Text = Display.Text + 3;
            Display.ForeColor = Color.Red;
        }

        private void Four_Click(object sender, EventArgs e)
        {
            //Display 4 in textboc when press 4 button with red color
            Display.Text = Display.Text + 4;
            Display.ForeColor = Color.Red;
        }

        private void Five_Click(object sender, EventArgs e)
        {
            //Display 5 in textboc when press 5 button with red color
            Display.Text = Display.Text + 5;
            Display.ForeColor = Color.Red;
        }

        private void Six_Click(object sender, EventArgs e)
        {
            //Display 6 in textboc when press 6 button with red color
            Display.Text = Display.Text + 6;
            Display.ForeColor = Color.Red;
        }

        private void Seven_Click(object sender, EventArgs e)
        {
            //Display 7 in textboc when press 7 button with red color
            Display.Text = Display.Text + 7;
            Display.ForeColor = Color.Red;
        }

        private void Eight_Click(object sender, EventArgs e)
        {
            //Display 8 in textboc when press 8 button with red color
            Display.Text = Display.Text + 8;
            Display.ForeColor = Color.Red;
        }

        private void Nine_Click(object sender, EventArgs e)
        {
            //Display 9 in textboc when press 9 button with red color
            Display.Text = Display.Text + 9;
            Display.ForeColor = Color.Red;
        }

        private void offbutton_Click(object sender, EventArgs e)//off button
        {
            disable(); //call disable to off calculator
        }

        private void On_Click(object sender, EventArgs e) //on button
        {
            enable(); //call enable function to on calculator
        }

        public void compute()
        {

            switch (count) //creating switch statement
            {
                 case 1:
                 answer = number + float.Parse(Display.Text);
                 //it performs addition

                 Display.Text = answer.ToString();              
                 //converts float into string
                 break;
                 case 2:
                     answer = number - float.Parse(Display.Text); 
//it performs subtraction
                     Display.Text = answer.ToString();              //converts float into string
                     break;
                 case 3:
                     answer = number * float.Parse(Display.Text);   //it performs Multiplication
                     Display.Text = answer.ToString();              //converts float into string
                     break;
                  case 4:
                      answer = number / float.Parse(Display.Text);   //it performs Division
                      Display.Text = answer.ToString();              //converts float into string
                      break;
                   default:
                      break;
                }

        }

        private void Add_Click(object sender, EventArgs e)
        {
            number = float.Parse(Display.Text);
            Display.Clear(); //clear the textbox
            Display.Focus(); //focus on textbox after clear
            count = 1; //count store case
            label1.Text = number.ToString() + "+"; //display text on lable
        }

        private void Sub_Click(object sender, EventArgs e)
        {
            number = float.Parse(Display.Text);
            Display.Clear(); //clear the textbox
            Display.Focus(); //focus on textbox after clear
            count = 2; //count store switch case value
            label1.Text = number.ToString() + "-"; //display text on lable

        }

        private void Multiply_Click(object sender, EventArgs e)
        {
            number = float.Parse(Display.Text);
            Display.Clear(); //clear the textbox
            Display.Focus(); //focus on textbox after clear
            count = 3; //count store switch case value
            label1.Text = number.ToString() + "*"; //display text on lable
        }

        private void Divide_Click(object sender, EventArgs e)
        {
            number = float.Parse(Display.Text);
            Display.Clear(); //clear the textbox
            Display.Focus(); //focus on textbox after clear
            count = 4; //count store switch case value
            label1.Text = number.ToString() + "/"; //display text on lable
        }

        private void equal_Click(object sender, EventArgs e)
        {
            compute();//call compute function to perform such operations
            label1.Text = "";//clear the text on the lable
        }

        private void clear_Click(object sender, EventArgs e)//clear button
        {
            Display.Text = ""; //clear the textbox
        }

        private void back_Click(object sender, EventArgs e)//backspace button
        {
            int length = Display.TextLength - 1;
            string text = Display.Text;
            Display.Clear();
            for (int i = 0; i < length; i++)
                Display.Text = Display.Text + text[i];

        }


    }
}

请尽快帮助我

2 个答案:

答案 0 :(得分:0)

如何使用

if(!float.TryParse(Display.Text, out number))
    return;

//rest of your code

答案 1 :(得分:-1)

使用Try {} catch {}

基本上你可以随处使用它。我建议您在使用logik时尽量使用它,以使您的程序尽可能稳定。它的主要任务是做错误处理(强烈推荐)。因此,当使用TryCatch(如下所示的示例)在catch块中执行某些操作时。显示MessageBox,显示自制的errorMessage或向用户显示ex.Message。

在你的情况下,我会实现它:

  1. 任何包含演员/解析的方法 - &gt;捕获了InvalidCastException
  2. 任何进行数学运算的方法
  3. 例如:

    try
    {
       //Do your logic here !
    }
    catch(Exception ex) //Here you can catch any type of Exception 
    {                   //like InvalidFormatException, and even print out ex.Message
       //Do something
       //Logging, MessageBox(Invalid operation ..), ...
    }
    

    请参阅https://msdn.microsoft.com/de-de/library/0yd65esw.aspx

    https://msdn.microsoft.com/library/ms229005%28v=vs.100%29.aspx了解更多信息。