简体   繁体   English

我应该把 try catch 块放在哪里? (简单的划分方法)

[英]Where i should place try catch block? (Simple division method)

It's my code:这是我的代码:

 using System;

 using System.Data;

 using System.Transactions;

 namespace BasicCourse.Exceptions 
 {

    class Program
    {
   
       static void Main(string[] args)
       {
       
       Division();

       Console.ReadKey();
       }

      static void Division()
      {
        Console.Write("Enter a first number: ");
        int firstEnteredNumber = int.Parse(Console.ReadLine());

        Console.Write("Enter a second number: ");
        int secondEnteredNumber = int.Parse(Console.ReadLine());

        float quotient = (float) firstEnteredNumber / secondEnteredNumber;

        Console.WriteLine("Result of division: " + quotient + "\n");
      }
}

} }

I'm learning the exceptions in C#.I want to attend the divide by zero exception but i don't know where should i place a try catch block.In Main method:我正在学习 C# 中的异常。我想参加除以零异常,但我不知道应该在哪里放置 try catch 块。在 Main 方法中:

try 
{ 
    Division() 
} 
catch (DivideByZeroException ex)
 etc...? 

Or inside the Division method?还是里面的Division

        static void Main(string[] args)
    {
        try
        {
            Division();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }


        Console.ReadKey();
    }

Dividing by zero doesn't result in an exception.除以零不会导致异常。 You would have to put in some logic to handle it.您将不得不放入一些逻辑来处理它。

            if (float.IsNaN(quotient))
        {
            //dostuff
        }

Update --- Sorry!更新---对不起!
I read others' answers and found that float don't throw any exceptions!I'm so sorry for I forgot it.我阅读了其他人的答案,发现 float 不会抛出任何异常!我很抱歉忘记了它。

DivideByZeroException Class DivideByZeroException 类
The exception that is thrown when there is an attempt to divide an integral or decimal value by zero.尝试将整数十进制值除以零时引发的异常。

The console shows:控制台显示:

Result of division: ∞除法结果:∞

In fact, we don't want this type of result, so I edited the answer.事实上,我们不想要这种类型的结果,所以我编辑了答案。


So we know the result is: no exception throwed.所以我们知道结果是:没有抛出异常。
If you want method Division() to remind users that they are trying to divide by 0, you can check the value.如果您希望方法 Division() 提醒用户他们正在尝试除以 0,您可以检查该值。

      static void Division()
      {
        Console.Write("Enter a first number: ");
        int firstEnteredNumber = int.Parse(Console.ReadLine());

        Console.Write("Enter a second number: ");
        int secondEnteredNumber = int.Parse(Console.ReadLine());

        //Added
        if (secondEnteredNumber == 0)
        {
            Console.WriteLine("cannot divide by 0");
        }
        else
        {
            float quotient = (float) firstEnteredNumber / secondEnteredNumber;
            Console.WriteLine("Result of division: " + quotient + "\n");
        }
      }

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

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