开发者

Help To Solve a Simple Try Catch Block Error

开发者 https://www.devze.com 2022-12-21 22:13 出处:网络
Im trying to use a try catch block But im facing some problems.. please help this is the code and the error which Im getting is

Im trying to use a try catch block But im facing some problems.. please help

this is the code and the error which Im getting is Error 1 The name 'Program' does not exist in the current context

using System;
namespace AddMinusDivideMultiply
{
    class Program
    {
        public static int i, j;

        public static void Main()
        {
            try
            {

                Console.Write("Please Enter The First Number  :");
                string temp = Console.ReadLine();
                i = Int32.Parse(temp);

                Console.Write("Please Enter The Second Number :");
                temp = Console.ReadLine();
                j = Int32.Parse(temp);

            }
            catch (Exception e)
            {
                Console.WriteLine(" An Execption was thrown: {0}", e.Message);
            }

            Terms.Minus(); 
        }
        }
    }

    class Terms
    {
        public static void Add()
        {
     开发者_Python百科       int add;
            add = Program.i + Program.j;
            Console.WriteLine("The Addition Of The First and The Second Number is {0}", add);
        }

        public static void Minus()
        {
        int minus;
        minus = Program.i - Program.j;
        Console.WriteLine("The Subraction Of The First and The Second Number is {0}", minus);
        }
    }


Please try AddMinusDivideMultiply.Program instead. However strange, you are already in the correct namespace.


You currently have a closing brace, making the Terms class outside of the AddMinusDivideMultiply namespace

Try this

using System;

namespace AddMinusDivideMultiply
{
    class Program
    {
        public static int i, j;

        public static void Main()
        {
            try
            {

                Console.Write("Please Enter The First Number  :");
                string temp = Console.ReadLine();
                i = Int32.Parse(temp);

                Console.Write("Please Enter The Second Number :");
                temp = Console.ReadLine();
                j = Int32.Parse(temp);

            }
            catch (Exception e)
            {
                Console.WriteLine(" An Execption was thrown: {0}", e.Message);
            }

            Terms.Minus(); 
        }
    }


    class Terms
    {
        public static void Add()
        {
            int add;
            add = Program.i + Program.j;
            Console.WriteLine("The Addition Of The First and The Second Number is {0}", add);
        }

        public static void Minus()
        {
        int minus;
        minus = Program.i - Program.j;
        Console.WriteLine("The Subraction Of The First and The Second Number is {0}", minus);
        }
    }
}


Your braces are not balanced, there are two braces closing the Main method where you want one.

0

精彩评论

暂无评论...
验证码 换一张
取 消