1

私は例外を再スローすることで多くの実験を行い、次の結論に達しました。

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

ループで再スローされた例外を処理しない場合、プログラムは終了します...つまり、これは、私が提示した上記の3つの結論が、再スローされた例外に関して正しいことを意味しますか?

4

1 に答える 1

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);
}
于 2012-12-03T23:00:29.003 に答える