tandem
tandem

Reputation: 2238

Doubts in try catch block in C#

the question was :

The application should ask the user for the total number of tickets to be booked. while the booking the tickets if the total number of booked tickets exceeds the available tickets, the application should raise an exception. I don't know why it is not showing an error when I do this I came up with this solution:

using System;    
namespace Ticket
{
    class blah
    {
        public void abc()
        {    
            int numberOfTickets;
            int numberOfAvailableTickets=10;
            int cost = 100;
            int pay;
            Console.WriteLine("how many tickets do you need");
            numberOfTickets = Convert.ToInt32(Console.ReadLine());

            try
            {
                if (numberOfTickets < numberOfAvailableTickets)
                {
                    pay = 100 * numberOfTickets;
                    Console.WriteLine("Pay please");
                    Console.WriteLine(pay);
                }                
            }                
            if( numberOfTickets>numberOfAvailableTickets)
            {                 
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
            }
        }
    }

}

 class Theater
 {
        static void Main(string[] args)
        {
            blah hi = new blah();
            hi.abc();
            Console.ReadLine();    
        }
  }
}

Upvotes: 1

Views: 240

Answers (5)

Matthew Flaschen
Matthew Flaschen

Reputation: 284927

The question is telling you to throw an exception if the booked number exceeds the available number.

So you don't need any try or catch in abc (I really hope these names aren't in your real code). You can also remove the if (numberOfTickets < numberOfAvailableTickets) (but keep the code inside.

Above:

pay = 100 * numberOfTickets;

move up:

if( numberOfTickets>numberOfAvailableTickets)
{

Inside the if put:

throw new ArgumentException("numberOfTickets is greater than numberOfAvailableTickets");

You can change ArgumentException to a custom exception if desired

Also note if you're using a catch, it must always be immediately after a try or another catch. You can't have an if between. See the documentation.

In Main, you can either catch that exception, or let it terminate the app (it's not clear from what you provided).

Upvotes: 2

Pankaj Upadhyay
Pankaj Upadhyay

Reputation: 13594

I would suggest you to remove the try & catch and simple use MessageBox.

if (numberOfAvaiableTickets < numberOfTickets)
{
    MessageBox.Show("Number of tickets exceeded", "ErrorWindow");
}
else
{
     pay = 100 * numberOfTickets;
     Console.WriteLine("Pay please");
     Console.WriteLine(pay);
}

Upvotes: 0

Dot NET
Dot NET

Reputation: 4907

The problem is that you didn't explicitly throw the exception. Unless you do that, the compiler sees nothing wrong with your code, as by default it would only throw exceptions which actually affect the running state of your program.

Although this is a 'quick fix' so to say, just adding a throw new Exception(); where you want the exception to be thrown will work.

However, ideally, I would recommend creating a custom Exception class for this purpose. But the previous line should work anyway :)

Upvotes: 0

aleroot
aleroot

Reputation: 72666

You have to use throw to raise an exception :

if( numberOfTickets>numberOfAvailableTickets)
     throw new Exception();

Upvotes: 2

Yahia
Yahia

Reputation: 70379

I am not even sure that the code you show even compiles... try this

using System;

namespace Ticket
{
    class blah
    {
        public void abc()
        {

            int numberOfTickets;
            int numberOfAvailableTickets=10;
            int cost = 100;
            int pay;
            Console.WriteLine("how many tickets do you need");
            numberOfTickets = Convert.ToInt32(Console.ReadLine());

            try
            {

            if( numberOfTickets>numberOfAvailableTickets)
                throw new Exception ("Not enough Tickets available!");


                    pay = 100 * numberOfTickets;
                    Console.WriteLine("Pay please");
                    Console.WriteLine(pay);

            }
            catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
        }
    }

}
    class Theater
    {
        static void Main(string[] args)
        {
            blah hi = new blah();
            hi.abc();
            Console.ReadLine();

        }
    }
}

It throws an Exception if the the entered number exceeds the available tickets...

Upvotes: 3

Related Questions