重新抛出异常并在循环中处理它......?

时间:2012-06-14 10:04:04

标签: exception c#-4.0 exception-handling rethrow

我通过重新抛出异常进行了大量实验,并得出了以下结论:

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);
            }
        }

然后在main()中:

            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);

如果我没有在循环中处理重新抛出的异常,我的程序会终止......所以这意味着以上三个结论对于重新抛出异常是正确的吗?

1 个答案:

答案 0 :(得分:1)

你的结论是正确的。我假设在结论1中你的意思是重新抛出异常会让你有机会记录错误。

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

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);
}