Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8933465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:32:37+00:00 2026-06-15T09:32:37+00:00

I experimented a lot with re throwing exceptions and came on to following conclusions:

  • 0

I experimented a lot with re throwing exceptions and came on to following conclusions:

1)We might want to re throw if we want to give more details about exception like (Inner exception)

2)we may re throw to entirely terminate the program ..

3)if we want to re throw and do not want to terminate the program then i will have to handle the re thrown exception in a loop …

i tried the following while doing this:

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

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

If i do not handle the re thrown exception in loop my program terminates … So does this means that above three Conclusions tat i presented are correct regarding re throwing exceptions??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:32:38+00:00Added an answer on June 15, 2026 at 9:32 am

    Your conclusions are correct. I am assuming that in conclusion 1 you mean that rethrowing an exception gives you a chance to log an error.

    But your approach is unconventional. Normally exceptions are not used to handle business logic cases. Why not modify your loop like this (replace you “try/catch” block)?

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've seen a lot of stuff about running code in subprocesses or threads, and
This is very easy for experimented Sql users i guess: given the following tables:
I have the following problem: I want to simulate some control engineering system. As
The code I am working on is throwing the aforementioned exception. I am not
So I keep hearing a lot about point free programming and I decided to
I've been programming a lot in standard C for about a two years now.
Fairly early on in my app, when I was a lot less experienced than
I am an experienced programmer learning Ruby (and liking it a lot). I'm working
I am getting ready to write lot of small experimental java programs as I
I'm trying to learn how to make & use packages in Java. I've experimented

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.