in my code Devide method is not working it gives an error "ExceptionHandling.Program.Devide(int, int)': not all code paths return a value"

When i change public int Devide method in to void,the programe is workig , plz explain me what is the wrong with my code, im new to programming , plz help me

class Program
    {
        public int Devide(int a, int b)
        {
            try
            {
                int k;
                k = a / b;
                return k;
            }

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

        }
        static void Main(string[] args)
        {
           int c;

            Program p = new Program();
           c= p.Devide(8,0);
          
            
        }
    }

You should check that the divisor (in your case variable b) is not zero.
You cannot divide by zero. you cannot do that in math, so why do you think a computer, which consists mainly of silicon(from sand), copper and plastic, is able to do that?
If you use void for a method, that method returns nothing. So don't use it if you want your method to return something.

You should check that the divisor (in your case variable b) is not zero.
You cannot divide by zero. you cannot do that in math, so why do you think a computer, which consists mainly of silicon(from sand), copper and plastic, is able to do that?
If you use void for a method, that method returns nothing. So don't use it if you want your method to return something.

yes i know we cant Divide some value bye zero,in math it will give infinity, thats why i use a DividebyZeroException(or Exception ) ,in line 8 it throws exception it should cath in 12,but this is not work for in if i int return type for the method, but try to do this exception handling with using a void it perfectly work,
Does Exception only work for void


here is the working code

class Program
    {
        public void Devide(int a, int b)// i use void not int
        {
            try
            {
                int c;
                c = a / b;
                Console.WriteLine(c);  
            }

            catch(DivideByZeroException e)
            {
                Console.WriteLine(e.Message);
            }
        }
        static void Main(string[] args)
        {
            Program p = new Program();
             p.Devide(4,0);
          
        }
    }

Why i cant use int and why i cant return some value

Try this:

class Program
    {
        static void Main(string[] args)
        {
            int result;
            result = Devide(4, 0); //calculate result
            Console.WriteLine(result); //write result
            Console.ReadKey(); //keep console on screen in debug mode
        }

         static int Devide(int a, int b)// i use void not int 
             // changed it to static so I don't need object reference
             // kept your spelling of the word divide
         {            
             try            
             {                
                 int c;                
                 c = a / b;
                 return c;
                 //These three lines could be replaced by : return a / b;
                 //Console.WriteLine(c);  //do this outside the method            
             }             
             catch(DivideByZeroException e)            
             {                
                 Console.WriteLine(e.Message); 
                 return -1; //must return something, some error code?
             }        
         }
    }

Try this:

class Program
    {
        static void Main(string[] args)
        {
            int result;
            result = Devide(4, 0); //calculate result
            Console.WriteLine(result); //write result
            Console.ReadKey(); //keep console on screen in debug mode
        }

         static int Devide(int a, int b)// i use void not int 
             // changed it to static so I don't need object reference
             // kept your spelling of the word divide
         {            
             try            
             {                
                 int c;                
                 c = a / b;
                 return c;
                 //These three lines could be replaced by : return a / b;
                 //Console.WriteLine(c);  //do this outside the method            
             }             
             catch(DivideByZeroException e)            
             {                
                 Console.WriteLine(e.Message); 
                 return -1; //must return something, some error code?
             }        
         }
    }

Thanks a lot ddanbe i found the error in my code , i didnt return -1 ; why do we return somgthing, Can you explain plz

Your code did work, because you did declare your method to return nothing, by using the keyword void . Void comes in handy when you want your method to return nothing. Say your program wants to explain something to a user, you would use a void method that uses a lot of Console.WriteLines etc. to write to the console.
But if you are performing a calculation, like with your Divide method, you should return the result of that calculation. If you devise such a method you must make sure that all code paths inside your method end in a return of whatever datatype your method returns to the main program.

Your code did work, because you did declare your method to return nothing, by using the keyword void . Void comes in handy when you want your method to return nothing. Say your program wants to explain something to a user, you would use a void method that uses a lot of Console.WriteLines etc. to write to the console.
But if you are performing a calculation, like with your Divide method, you should return the result of that calculation. If you devise such a method you must make sure that all code paths inside your method end in a return of whatever datatype your method returns to the main program.

Thanks a lot ddanbe, i understood Thanks again

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.