none
Try Catch RRS feed

  • Question

  • Hi all, I have the following function: - i would like to add try catch statement to handle negative numbers. Would anyone help me on this?

    public int test(int CharV)

    int Numb1 = 0;  int Numb2 = 1;  int Numb3 = 2;
    while (CharV > Numb1)  
    {   
    Numb2 = CharV * Numb2;   
    CharV -= 1;  
    }  
    return Numb2;
    }

    Tuesday, August 28, 2018 8:11 AM

Answers

  • Hi all, I have the following function: - i would like to add try catch statement to handle negative numbers. Would anyone help me on this?

    public int test(int CharV)

    int Numb1 = 0;  int Numb2 = 1;  int Numb3 = 2;
    while (CharV > Numb1)  
    {   
    Numb2 = CharV * Numb2;   
    CharV -= 1;  
    }  
    return Numb2;
    }

    class Program
    {
        static void Main(string[] args)
        {
            int z = -5;
            try { 
            test(z);
            }
            catch (ArithmeticException e)
            {
                Console.WriteLine($"{e.GetType().Name}: Negative numbers not allowed!");
            }
            Console.ReadLine();
        }
    
        static public int test(int CharV)
        //public int test(int CharV)
        {
            if(CharV < 0) throw new ArithmeticException();
            
            int Numb1 = 0; int Numb2 = 1; int Numb3 = 2;
            while (CharV > Numb1)
            {
                Numb2 = CharV * Numb2;
                CharV -= 1;
            }
            return Numb2;
        }
    }
    
    

    - Wayne

    • Marked as answer by Milli_22 Tuesday, August 28, 2018 12:02 PM
    Tuesday, August 28, 2018 9:21 AM

All replies

  • You need a try/catch for what? The code is not doing anything that would warrant a try/catch.
    Tuesday, August 28, 2018 9:13 AM
  • Please read try-catch first.

    Cause try-catch is for structured error handling (SEH), not for parameter validation.

    Thus there are two possible approaches:

    namespace ConsoleCS
    {
        using System;
    
        class Program
        {
            static void Main(string[] args)
            {
                int parameter = 13;
                int result = -999;
                if (Test1(parameter, out result))
                {
                    Console.WriteLine($"Result for {parameter} = {result}");
                }
                else
                {
                    Console.WriteLine($"Could not compute for {parameter}.");
                }
    
                parameter = -13;
                result = -999;
                if (Test1(parameter, out result))
                {
                    Console.WriteLine($"Result for {parameter} = {result}");
                }
                else
                {
                    Console.WriteLine($"Could not compute for {parameter}.");
                }
    
                try
                {
                    parameter = 13;
                    result = Test2(parameter);
                    Console.WriteLine($"Result for {parameter} = {result}");
                }
                catch (ArgumentException exception)
                {
                    Console.WriteLine($"Could not compute for {parameter}, exception is: {exception.Message}");
                }
    
                try
                {
                    parameter = -13;
                    result = Test2(parameter);
                    Console.WriteLine($"Result for {parameter} = {result}");
                }
                catch (ArgumentException exception)
                {
                    Console.WriteLine($"Could not compute for {parameter}, exception is: {exception.Message}");
                }
    
                Console.WriteLine("Done.");
                Console.ReadLine();
            }
    
            public static bool Test1(int number, out int result)
            {
                result = 1;
                if (number > 0)
                {
                    int number1 = 0;
                    while (number > number1)
                    {
                        result = number * result;
                        number -= 1;
                    }
    
                    return true;
                }
                else
                {
                    return false;
                }
            }
    
            public static int Test2(int number)
            {
                if (number < 0)
                {
                    throw new ArgumentException("Number must be positive or 0.");
                }
    
                int result = 1;
                int number1 = 0;
                while (number > number1)
                {
                    result = number * result;
                    number -= 1;
                }
    
                return result;
            }
        }
    }
    

    Tuesday, August 28, 2018 9:21 AM
  • Hi all, I have the following function: - i would like to add try catch statement to handle negative numbers. Would anyone help me on this?

    public int test(int CharV)

    int Numb1 = 0;  int Numb2 = 1;  int Numb3 = 2;
    while (CharV > Numb1)  
    {   
    Numb2 = CharV * Numb2;   
    CharV -= 1;  
    }  
    return Numb2;
    }

    class Program
    {
        static void Main(string[] args)
        {
            int z = -5;
            try { 
            test(z);
            }
            catch (ArithmeticException e)
            {
                Console.WriteLine($"{e.GetType().Name}: Negative numbers not allowed!");
            }
            Console.ReadLine();
        }
    
        static public int test(int CharV)
        //public int test(int CharV)
        {
            if(CharV < 0) throw new ArithmeticException();
            
            int Numb1 = 0; int Numb2 = 1; int Numb3 = 2;
            while (CharV > Numb1)
            {
                Numb2 = CharV * Numb2;
                CharV -= 1;
            }
            return Numb2;
        }
    }
    
    

    - Wayne

    • Marked as answer by Milli_22 Tuesday, August 28, 2018 12:02 PM
    Tuesday, August 28, 2018 9:21 AM
  • @Wayne: For these kinds of test, we should use ArgumentException:

    ArgumentException is thrown when a method is invoked and at least one of the passed arguments does not meet the parameter specification of the called method.
    Tuesday, August 28, 2018 9:27 AM
  •  For these kinds of test, we should use ArgumentException:

    That would be a better fit, and would avoid confusion if an Arithmetic 
    exception gets thrown for some reason other than a negative argument.

    ArithmeticException:

    "The exception that is thrown for errors in an arithmetic, casting, or 
    conversion operation."

    - Wayne

    Tuesday, August 28, 2018 9:49 AM
  • ArgumentOutOfRangeException might be another suitable choice here:

    "The exception that is thrown when the value of an argument is outside
    the allowable range of values as defined by the invoked method."

    - Wayne

    Tuesday, August 28, 2018 10:36 AM
  • Yup, and as we don't calculate, cast or convert something, ArthimeticException is the wrong one.
    Tuesday, August 28, 2018 11:07 AM
  • Well, seems you cannot read a thread, cause this is the original algo.

    Wednesday, August 29, 2018 8:42 AM