Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Exception 重新引发异常并在循环中处理它。。。?_Exception_C# 4.0_Exception Handling_Rethrow - Fatal编程技术网

Exception 重新引发异常并在循环中处理它。。。?

Exception 重新引发异常并在循环中处理它。。。?,exception,c#-4.0,exception-handling,rethrow,Exception,C# 4.0,Exception Handling,Rethrow,我多次尝试重新抛出异常,并得出以下结论: 1) 如果我们想提供更多关于异常的细节,比如(内部异常),我们可能需要重新抛出 2) 我们可能会重新抛出以完全终止程序 3) 如果我们想重新抛出并且不想终止程序,那么我必须在循环中处理重新抛出的异常 执行此操作时,我尝试了以下操作: public void input() { char opt; int id; int qty; int in

我多次尝试重新抛出异常,并得出以下结论:

1) 如果我们想提供更多关于异常的细节,比如(内部异常),我们可能需要重新抛出

2) 我们可能会重新抛出以完全终止程序

3) 如果我们想重新抛出并且不想终止程序,那么我必须在循环中处理重新抛出的异常

执行此操作时,我尝试了以下操作:

 public void input()
        {
            char opt;
            int id;
            int qty;
            int ind = -1;

            do
            {
                Console.WriteLine("Add item to  cart:   ");
                opt = Convert.ToChar(Console.ReadLine());
                if (opt == 'n' || opt == 'N')
                    break;
                else
                {
                    cashier.showProductList();
                    Console.WriteLine("Enter Product ID from above list:    ");
                    id = Convert.ToInt32(Console.ReadLine());
                    foreach (Products item in cashier.ProductList)
                    {
                        if (id == item.ProductID)
                        {
                            BoughtItems.Add(item);
                            ind = Cashier.ProductList.IndexOf(item);                        
                        }
                    }

                    Console.WriteLine("Enter The quantity required:     ");
                    qty = Convert.ToInt32(Console.ReadLine());
                    try
                    {
                        if (qty < 0)
                        {
                            throw new BuisnessException("Qty can not be -ve...", new Exception("enter a valid +ve amount"));
                        }
                        else if (qty < cashier.ProductList[ind].Quantity)
                        {
                            BoughtItems[BoughtItems.Count - 1].Quantity = qty;
                            TotalPrice = TotalPrice + (BoughtItems[BoughtItems.Count - 1].Quantity * BoughtItems[BoughtItems.Count - 1].price);
                        }
                        else if (qty >= cashier.ProductList[ind].Quantity)
                        {
                            throw new QuantityExceedingStockException("Item not in sufficient amount", new Exception("Enter quantity less than" + Cashier.ProductList[ind].Quantity));
                        }
                    }
                    catch (BuisnessException ex)
                    {

                        BoughtItems.RemoveAt(BoughtItems.Count - 1);
                        Console.WriteLine(ex.Message);
                        throw;
                        //throw; //Re throwing terminating the i/p loop if re thrown exception not handled in loop! :/
                    }
                    catch (QuantityExceedingStockException ex)
                    {
                        BoughtItems.RemoveAt(BoughtItems.Count - 1);
                        Console.WriteLine(ex.Message);
                        throw;
                    }
                }

            } while (true);
        }

        public void showCartItems()
        {
            foreach (Products item in _boughtItems)
            {
                Console.WriteLine(item.ProductID + "  " + item.name + "  " + item.price + "  " + item.Quantity);
            }
        }

如果我不处理循环中重新抛出的异常,我的程序将终止。。。那么这是否意味着我提出的上述三个关于重新抛出异常的结论是正确的呢?

你的结论是正确的。我假设在结论1中,您的意思是重新引用异常会给您一个记录错误的机会

但你的方法是非传统的。通常情况下,异常不用于处理业务逻辑案例。为什么不这样修改循环(替换“try/catch”块)

if(数量<0)
{
BoughtItems.RemoveAt(BoughtItems.Count-1);
//下面的代码已被编辑以显示在同一块中引发异常
//作为相关逻辑。
string message=“数量不能为-ve,请输入有效的+ve金额”;//已编辑
控制台写入线(消息);
抛出新业务异常(消息);//已编辑
}
否则如果(数量<出纳.产品列表[ind].数量)
{
BoughtItems[BoughtItems.Count-1]。数量=数量;
TotalPrice=TotalPrice+(BoughtItems[BoughtItems.Count-1]。数量*BoughtItems[BoughtItems.Count-1]。价格);
}
否则,如果(数量>=出纳.ProductList[ind].数量)
{
BoughtItems.RemoveAt(BoughtItems.Count-1);
Console.WriteLine(“金额不足的项目,请输入小于“+Cashier.ProductList[ind].quantity”的数量);
}

你的结论是正确的。我假设在结论1中,您的意思是重新引用异常会给您一个记录错误的机会

但你的方法是非传统的。通常情况下,异常不用于处理业务逻辑案例。为什么不这样修改循环(替换“try/catch”块)

if(数量<0)
{
BoughtItems.RemoveAt(BoughtItems.Count-1);
//下面的代码已被编辑以显示在同一块中引发异常
//作为相关逻辑。
string message=“数量不能为-ve,请输入有效的+ve金额”;//已编辑
控制台写入线(消息);
抛出新业务异常(消息);//已编辑
}
否则如果(数量<出纳.产品列表[ind].数量)
{
BoughtItems[BoughtItems.Count-1]。数量=数量;
TotalPrice=TotalPrice+(BoughtItems[BoughtItems.Count-1]。数量*BoughtItems[BoughtItems.Count-1]。价格);
}
否则,如果(数量>=出纳.ProductList[ind].数量)
{
BoughtItems.RemoveAt(BoughtItems.Count-1);
Console.WriteLine(“金额不足的项目,请输入小于“+Cashier.ProductList[ind].quantity”的数量);
}

非常感谢。。但那会导致多个其他的如果,对吗。。相反,我们可以使用exception类来处理类似类型的错误,这不会使代码混乱。上面的代码与您的代码做相同的事情,只是它不会中断循环。如果您仍然需要异常,最好将其与其他相关逻辑放在相同的代码块中。我编辑了上面的代码作为示例。非常感谢。。但那会导致多个其他的如果,对吗。。相反,我们可以使用exception类来处理类似类型的错误,这不会使代码混乱。上面的代码与您的代码做相同的事情,只是它不会中断循环。如果您仍然需要异常,最好将其与其他相关逻辑放在相同的代码块中。我编辑了上面的代码作为示例。
            do
        {
            try
            {
                cashier1.Customr.input();
            }
            catch (BuisnessException ex)
            {
                //Console.WriteLine(ex.Message);
                Console.WriteLine(ex.InnerException.Message);
            }
            catch (QuantityExceedingStockException ex)
            {
                Console.WriteLine(ex.InnerException.Message);
            }
            //catch
            //{
            //    Console.WriteLine("Press y below to continue shopping");
            //}
            Console.Write("Continue Shopping ?");
            opt = Convert.ToChar(Console.ReadLine());
            if (opt == 'n')
                break;
            else
                continue;
        } while (true);
        Console.WriteLine("Total Price is:  " + cashier1.Customr.TotalPrice);
if (qty < 0)
{
    BoughtItems.RemoveAt(BoughtItems.Count - 1);
    // The code below has been EDITED to show throwing an exception in the same block
    // as the related logic.
    string message = "Qty can not be -ve, enter a valid +ve amount"; // EDITED
    Console.WriteLine(message);
    throw new BusinessException(message); // EDITED
}
else if (qty < cashier.ProductList[ind].Quantity)
{
    BoughtItems[BoughtItems.Count - 1].Quantity = qty;
TotalPrice = TotalPrice + (BoughtItems[BoughtItems.Count - 1].Quantity * BoughtItems[BoughtItems.Count - 1].price);
}
else if (qty >= cashier.ProductList[ind].Quantity)
{
    BoughtItems.RemoveAt(BoughtItems.Count - 1);
    Console.WriteLine("Item not in sufficient amount, enter quantity less than " + Cashier.ProductList[ind].Quantity);
}