By GokiSoft.com| 14:33 24/05/2021|
C Sharp

[Share Code] Tìm hiểu về try catch - exception Lập trình C# - Khoá học lập trình C#



using System;
using Lesson05.Modals;

namespace Lesson05
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            //Test();
            //Console.WriteLine("Nhap x = ");
            //int x = 0;
            //while(true)
            //{
            //    try
            //    {
            //        x = Int32.Parse(Console.ReadLine());
            //        break;
            //    } catch(Exception e)
            //    {
            //        Console.WriteLine("Yeu cau nhap lai x (so nguyen) = ");
            //    }
            //}
            int x = ReadInt("x");

            //Fix logic => goc error chia 0 -> yeu cau nguoi dung nhap y <> 0
            int y = 0;
            //Console.WriteLine("Nhap y = ");
            while (true)
            {
                //while (true)
                //{
                //    try
                //    {
                //        y = Int32.Parse(Console.ReadLine());
                //        break;
                //    }
                //    catch (Exception e)
                //    {
                //        Console.WriteLine("Yeu cau nhap lai y (so nguyen) = ");
                //    }
                //}
                y = ReadInt("y");
                if (y != 0) break;
                else Console.WriteLine("Yeu cau nhap lai y (y <> 0): ");
            }

            /**try
            {
                int s = x / y;
                Console.WriteLine("s = {0}", s);
            } catch(Exception e)
            {
                Console.WriteLine(e.StackTrace);
            } finally
            {
                //Thanh cong + That bai -> de xu ly code nay???
            }*/
            if(y == 0)
            {
                //Console.WriteLine("Yeu cau nhap y <> 0");
                throw new Exception("Loi chia cho 0");
            } else
            {
                int s = x / y;
                Console.WriteLine("s = {0}", s);
            }
        }

        static int ReadInt(string varname)
        {
            Console.WriteLine("Nhap {0} = ", varname);
            int x = 0;
            while (true)
            {
                try
                {
                    x = Int32.Parse(Console.ReadLine());
                    break;
                }
                catch (Exception e)
                {
                    Console.WriteLine("Yeu cau nhap lai {0} (so nguyen) = ", varname);
                }
            }

            return x;
        }

        static void Test()
        {
            Console.WriteLine("Hello World!");
            Student std = new Student();
            std.Input();

            Animal animal = new Animal();
            animal.Input();
        }
    }
}

namespace Lesson05.Modals
{
    class Student
    {
        public string Fullname { get; set; }
        public string Address { get; set; }

        public Student()
        {

        }

        public void Input()
        {
            Console.WriteLine("Nhap du lieu");
        }
    }

    class Animal
    {
        public string Name { get; set; }

        public void Input()
        {
            Console.WriteLine("Nhap thong tin dong vat");
        }
    }
}




Tags:

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)