Not all code paths return a value

If you return any thing from method instead of void then you must return the defined type value as described in return type.

Case 1. If you are using for , while loop or any other conditional statement then you should use return statement in all places like below.

public static bool isTwenty(int num)
        {
            for (int j = 1; j <= 20; j++)
            {
                if (num % j != 0)
                {
                    return false;
                }
                else if (num % j == 0 && num == 20)
                {
                    return true;
                }
            }
            return false;

        }

Case 2. If you are using Try Catch then Give return or throw in both try and catch Or give in finally only as below.

put return or throw in Try and catch both


public int display() {
            int x = 0;
            int y = 10;
            try
            {
                int z = y / x;
                return 0;
            }

            catch (DivideByZeroException)
            {
                Console.WriteLine("DivideByZeroException");
                //throw;
                return 1;
            }
            catch (Exception)
            {
                Console.WriteLine("general exception");
                //throw;
                return 2;
            }
            finally
            {
                Console.WriteLine("finally");
               // throw new Exception("bhanu");

            }

Put return or throw in finally only as below

public int display() {
            int x = 0;
            int y = 10;
            try
            {
                int z = y / x;
                //return 0;
            }

            catch (DivideByZeroException)
            {
                Console.WriteLine("DivideByZeroException");
                //throw;
                //return 1;
            }
            catch (Exception)
            {
                Console.WriteLine("general exception");
                //throw;
                //return 2;
            }
            finally
            {
                Console.WriteLine("finally");
                throw new Exception("bhanu");
            }
        }

Comments

Popular posts from this blog

Swagger for Azure functions: Undocumented TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body. Fixed.

Fixed: The required column was not present in the results of a 'FromSql' operation in asp.net core EF core

Severity Code Description Project File Line Suppression State Error Unable to resolve dependencies. 'Xamarin.Android.Support.Design 24.2.1' is not compatible with 'Xamarin.Forms 2.3.3.180 constraint: Xamarin.Android.Support.Design (= 23.3.0)'. 0