除以零的例外 [英] exception for dividing by zero

查看:135
本文介绍了除以零的例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自定义计算器应用程序.
我遇到了麻烦:

i am working on a custom calculator app.
i am having trouble with:

decimal tb1;
            decimal tb2;
            decimal answer;

            tb1 = decimal.Parse(textBox1.Text);
            tb2 = decimal.Parse(textBox2.Text);

            answer = tb1 / tb2;
            textBox1.Text = (answer.ToString());
            textBox2.Text = "0";


当textbox2为0时,我希望显示一条消息,说不可能被零除,但我不知道您要使用try and catch.


when textbox2 is 0 i would like a message to show up saying that dividing by zero is not possible, but i do not know you to use try and catch.

推荐答案

首先,可以使用TryParse代替Parse.

使用try/catch并不困难.此外,不使用结构化异常处理进行编程就没有任何意义.

在UI上正确使用异常是很特别的事情.请参见:
我如何制作滚动条到达底部时将停止的循环 [
First, you can use TryParse instead of Parse.

There is nothing difficult in using try/catch. Moreover, programming without using of structured exception handling makes little sense.

Using exceptions with UI correctly is something special. Please see this:
How do i make a loop that will stop when a scrollbar reaches the bottom[^].

You can (and often) should parse exception locally as well. The pattern is very simple:

try {
    Evaluate(); //whatever you calculate
} catch (System.Format e) {
    ShowFormatProblem(); //select text box with problem so, show error message
} catch (System.ArithmeticException e) {
    ShowException(); //write some function to select textBox1 in question, show error message, etc.
}



注意:它将仅捕获少量异常类:System.FormatSystem.Arithmetic异常和派生类.所有其他异常都将传播到堆栈的顶部,并被捕获在Application级别(System.Windows.Forms或WPF,请参见上面的参考)中.

重要的是不要阻止异常传播.您的情况是少数情况下的一种情况,您可以仅针对少数几个异常类将其阻止.当您静默捕获并阻止所有异常时,这种情况更为罕见.通常,这样做是为了解决第三方软件无法修补的某些软件缺陷.

—SA



Pay attention: it will only catch few exception classes: System.Format, System.Arithmetic exceptions and derived classes. All other exception will propagate top of the stack and will be caught in the level of Application (System.Windows.Forms or WPF, see the above reference).

It is very important not to block exception from propagation. Your case in one of the rare cases when you can block it for just a few exception classes. It it even much more rare case when you catch and block all exceptions silently; usually this is done to work around some software defect in third-party software not accessible for patching.

—SA


因此try块包含可能导致异常的受保护代码.该块将一直执行直到引发异常或成功完成.

catch子句可以不带参数使用,在这种情况下,它可以捕获任何类型的异常,并称为常规catch子句.它还可以接受从System.Exception派生的对象参数,在这种情况下,它处理特定的异常.

So the try block contains the guarded code that may cause the exception. The block is executed until an exception is thrown or it is completed successfully.

The catch clause can be used without arguments, in which case it catches any type of exception, and referred to as the general catch clause. It can also take an object argument derived from System.Exception, in which case it handles a specific exception.

decimal tb1;
decimal tb2;
decimal answer;
            try
            {
                tb1 = decimal.Parse(textBox1.Text);
                tb2 = decimal.Parse(textBox3.Text);
                answer = tb1 / tb2;
                textBox1.Text = String.Format("{0:0.0}", answer);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Fail due to division by zero");
                string excpt = ex.InnerException.ToString();
            }



在这里,您会得到一个例外,答案为= tb1/tb2;因此,它将直接转到catch块,而无需处理十进制到字符串的转换.

但是您在我提供的代码中将文本框值解析为十进制时会遇到异常.在那里,您必须检查或避免用户输入字母.



Here you will get an exception in answer = tb1 / tb2; so then it will directly go to catch block without processing the conversion of decimal to string.

But you in the code that I have provided you can get an exception while parsing the textbox values to decimal. There you have to check or avoid user input for letters.


If(tb2!= 0)
{
tb1/tb2;
}

等等...
If(tb2 != 0)
{
tb1 / tb2;
}

etc...


这篇关于除以零的例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆