繁体   English   中英

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

[英]Where to use exception handling in calculator?

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

我的计算器.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];

        }


    }
}

请尽快帮我解决

怎么用

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

//rest of your code

使用Try {} catch {}

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

在你的情况下,我会为:

  1. 任何包含强制转换/解析的方法 -> InvalidCastException 都会被捕获
  2. 任何进行数学运算的方法

例如:

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了解更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM