I have done an exercise to write a program that prompts a user to enter two numbers. The program should divide one number by another. Use a try-catch block to prevent the user from dividing by zero.
When I debug this code it does not return an answer when the method is called (e.g 8/2)
namespace classProgram
{
    class myClass
    {
        static void Main(string[] args)
        {
            //create a new myClass object
            myClass party = new myClass();
            Console.WriteLine("Please enter a number");
            int num1 = Int32.Parse(Console.ReadLine());
            Console.WriteLine("Please enter a second number");
            int num2 = Int32.Parse(Console.ReadLine());
            Console.ReadLine();
            //this is where an error is most likely to occur
            try
            {
                num2 = 0;
                party.DivideNum(num1,num2); //please explain why my program does not return a result? 
            }
            //specific exception to be caught
            catch (DivideByZeroException e)
            {
                Console.WriteLine("{0} You cannot divide by zero", e);
            }
            //least specific exception
            catch (Exception e)
            {
                Console.WriteLine("{0} second exception caught:",e);
            }
        }
        public int DivideNum( int x,int y)
        {
            if (y==0)
            {
               throw new Exception();
            }
            else
            {
                return x/y; //WHY DOES THE CODE NOT RETURN AN ANSWER WHEN I DEBUG?
            }
        }  
    }
}

What result should the computer return?
Computers cannot and never will do a divide by zero.
An error is generated and you can catch it with code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.