Пример #1
0
        static void Main(string[] args)
        {
            DemoCode demo = new DemoCode();

            try
            {
                int result = demo.GrandparentMethod(4);
                Console.WriteLine($"The value at the given position is { result }");
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine("You gave bad information. Bad user!");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                var inner = ex.InnerException;

                while (inner != null)
                {
                    Console.WriteLine(inner.StackTrace);
                    inner = inner.InnerException;
                }
            }


            Console.ReadLine();
        }
Пример #2
0
        static void Main()
        {
            DemoCode demo = new DemoCode();

            try
            {
                int result = demo.GrandParentMethod(4);
                Console.WriteLine($"The value at the given position is: {result}");
            }
            catch (Exception ex)
            {
                // Using ex to get more information about the Exception itself
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);

                var inner = ex.InnerException;

                while (inner != null)
                {
                    Console.WriteLine(inner.StackTrace);
                    inner = inner.InnerException;
                }
            }

            Console.ReadLine();
        }
Пример #3
0
        static void Main(string[] args)
        {
            //When you type the word "DemoCode" it gives you the ability to link to class libraries that you haven't already linked.

            //The code just below instantiates demo code
            DemoCode demo = new DemoCode();

            //Recall from the ExceptionsLibary class, that when we want to call any of the method in it, we need to specify a number as a parameter
            //The number refers to the position in an array that we created
            //When we input the number parameter when calling the method it'll return to us the item in that particular position in the array we created
            //Simply put, by writing "3" in the "demo.GrandParentMethod(3);" we're requesting for the fourth item in the array we created in the ExceptionsLibrary class.
            //Arrays start counting from position 0 (which is the first item) as such 3 is the fourth position if you follow the n+1 principle.
            //int result = demo.GrandParentMethod(3);

            //When we specify a unit parameter that is beyond what the length of the array you get an out of bounds exception.
            //Note that variables created within blocks (curly braces) are limited in scope to that block.
            //If you need to be able to access the variable outside the block you must first declare the variable globally (outside the block).
            try
            {
                int result = demo.GrandParentMethod(4);
                Console.WriteLine($"The value at the given position is {result}");
            }
            //Note that you can catch multiple types of error within one try catch usage.
            //Make sure the "Exception" is the last exception type to catch because it's very general
            //Note that the parameter "ex" is a variable parameter that can be used repeatedly because it is limited in scope to the catch block it is mentioned in.
            catch (ArgumentException ex)
            {
                Console.WriteLine("You gave us suspicious information, are you a hoodlum?");
            }
            catch (Exception ex)
            {
                //Try catch should be done as high as possible in the program so as to ensure that you get as much information as possible about the root of the problem and every other place that is affected by the problem.
                //Low level classes don't need try catch as much as high level classes.
                //If you don't have a throw here it means the application can continue to run in spite of the error.
                Console.WriteLine(ex.Message);
                //The line of code below shows you the stack trace
                Console.WriteLine(ex.StackTrace);

                //The codes below help get the inner exception (which is the rest of the stack trace)
                //As long as there's still an inner exception it will write out the stack trace.
                var inner = ex.InnerException;

                while (inner != null)
                {
                    Console.WriteLine(inner.StackTrace);
                    inner = inner.InnerException;
                }
            }


            Console.ReadLine();
        }
Пример #4
0
        public IResult Delete(DemoCode entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException();
            }

            IResult result = new Result();

            _demoCodeRepos.Delete(entity);
            result.Success = true;

            return(result);
        }
Пример #5
0
        public async Task <IActionResult> OnGetAsync(Guid?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            DemoCode = _demoCodeService.Get(id.Value);

            if (DemoCode == null)
            {
                return(NotFound());
            }
            return(Page());
        }
Пример #6
0
        public async Task <IActionResult> OnGetAsync(Guid?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            DemoCode = await _context.DemoCode.FirstOrDefaultAsync(m => m.ID == id);

            if (DemoCode == null)
            {
                return(NotFound());
            }
            return(Page());
        }
Пример #7
0
        static void Main(string[] args)
        {
            DemoCode demo = new DemoCode();

            try
            {
                int result = demo.GrandParentMethod(4);
                Console.WriteLine($"The value at the given position is {result}");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
            Console.ReadLine();
        }
Пример #8
0
        public IActionResult OnPostAsync(Guid?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            DemoCode = _demoCodeService.Get(id.Value);

            if (DemoCode != null)
            {
                _demoCodeService.Delete(DemoCode);
            }

            return(RedirectToPage("./Index"));
        }
Пример #9
0
        static void Main(string[] args)
        {
            DemoCode demo = new DemoCode();

            try
            {
                int res = demo.gpm(4);
                Console.WriteLine($"Value at position is {res}");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }

            Console.ReadLine();
        }
Пример #10
0
        static void Main(string[] args)
        {
            DemoCode demoCode = new DemoCode();

            try
            {
                Console.WriteLine("Open DataBase Connections");

                var position = 10;
                if (position > 0)
                {
                    Console.WriteLine($"this value at the givem position is {demoCode.GrandParentMethod(position)}");
                }
                else
                {
                    throw new IndexOutOfRangeException("Please pass a value bigger the 0");
                }
            }
            catch (IndexOutOfRangeException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (ArgumentException)
            {
                Console.WriteLine("You give us a bad information, bad user");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);

                while (ex.InnerException != null)
                {
                    Console.WriteLine(ex.StackTrace);
                }
            }
            finally
            {
                Console.WriteLine("Close all DataBase connections");
            }
            Console.ReadLine();
        }
Пример #11
0
        public IResult Update(DemoCode entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException();
            }

            IResult result = new Result();

            var targetData = this.Get(entity.ID);

            if (targetData != null)
            {
                _demoCodeRepos.Update(targetData);

                result.Success = true;
            }
            else
            {
                result.Success = false;
            }

            return(result);
        }
Пример #12
0
        public static IEnumerable<DemoCode> GetDemos()
        {
            var res = new List<DemoCode>();
            DemoCode demo;

            demo = new DemoCode("Read Asyncronous", "Basic/Read");
            demo.Files.Add(new DemoFile());
            demo.LastFile.Contents = "Bla bla .bla";
            res.Add(demo);

            // Events
            demo = new DemoCode("Implementing INotifyRead", "Events/Interfaces");
            demo.Files.Add(new DemoFile());
            demo.LastFile.Contents = "Bla bla .bla";
            res.Add(demo);

            demo = new DemoCode("Implementing INotifyWrite", "Events/Interfaces");
            demo.Files.Add(new DemoFile());
            demo.LastFile.Contents = "Bla bla .bla";

            res.Add(demo);

            return res;
        }
Пример #13
0
        static void Main(string[] args)
        {
            // ############################ 1. Try catch ####################################
            DemoCode demo = new DemoCode();

            try
            {
                int result = demo.GrandparentMethod(4);
                Console.WriteLine($"The value at the given position is { result }");
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine("You gave us bad information. Bad user!");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                var inner = ex.InnerException;

                while (inner != null)
                {
                    Console.WriteLine(inner.StackTrace);
                    inner = inner.InnerException;
                }
            }

            Console.ReadLine();



            // ############################ 2. Try catch ####################################

            /* try {
             *
             *   StreamReader file = new StreamReader("file.txt"); // 1. Block of code. We think it's going to give us an error
             *
             *       // ..... More code errors can occur here as well
             *
             * } catch (FileNotFoundException)  {// just for the file not found
             * Debug.WriteLine("File name not found!");  // In this case after starting thsi aplication it will throw an messagae 'File name not found?
             *
             * } catch (Exception) {
             *  // throw;   // 2. Block of code is useful for only when the error occurs. Ex: If the file above doesn't exist then it will throw an errow here
             * Debug.WriteLine("An Error occured!"); // Error message examplo
             * }
             *
             *
             *
             *
             * // ############################ 3. Try catch finally ####################################
             *
             * StreamReader file = null;
             * try
             * {
             *
             *  file = new StreamReader("file.txt"); // 1. Block of code. We think it's going to give us an error
             *
             *  // ..... More code errors can occur here as well
             *
             * }
             * catch (FileNotFoundException)
             * {// just for the file not found
             *  Debug.WriteLine("File name not found!");  // In this case after starting thsi aplication it will throw an messagae 'File name not found?
             *
             * }
             * catch (Exception)
             * {
             *  // throw;   // 2. Block of code is useful for only when the error occurs. Ex: If the file above doesn't exist then it will throw an errow here
             *  Debug.WriteLine("An Error occured!"); // Error message examplo
             * } finally
             *          {
             *  if(file != null)   file.Dispose();   // 3. Liberate the file when you don't want it anymore open
             *          } */
        }