三、C#笔记

/// <summary>
/// 第五章:使用复合赋值和循环语句
/// </summary>
namespace Chapter5
{
    class Program
    {
        static void Main(string[] args)
        {
            One();//5.1使用复合赋值操作符
            Two();//5.2编写while语句
            Three();//5.3编写for语句
            Tour();//5.4编写do语句
            Console.ReadLine();
        }

        private static void Tour()
        {
            int i = 0;
            do
            {
                Console.WriteLine(i);
                i++;
            }
            while (i < 10);
        }

        private static void Three()
        {
            //for语句三个部分都可以省略,布尔表达式默认为true
            for(int i = 0; i < 10; i++)
            {
                Console.WriteLine(i);
            }
        }

        private static void Two()
        {
            int i = 0;
            while (i < 10)
            {
                Console.WriteLine(i);
                i++;
            }
        }

        private static void One()
        {
            int answer=0;
            Console.WriteLine(answer += 42);
        }
    }
}
 

/// <summary>
/// 第六章:管理错误和异常
/// </summary>
namespace Chapter6
{
    class Program
    {
        static void Main(string[] args)
        {
            //try catch的使用
            Moudle.Class1 First = new Moudle.Class1 ();
            First.Test();
            //checked、unchecked
            Moudle.Class2 Second = new Moudle.Class2();
            Second.Test();
            //抛出异常
            Moudle.Class3 Third = new Moudle.Class3();
            Third.Test();
            //6.4使用finally块
            Four();
            Console.ReadLine();
        }

        private static void Four()
        {
            try
            {

            }
            finally
            {
                //无论怎样都会执行
            }
        }
    }
}
 

/// <summary>
/// 6.1尝试执行代码并捕捉异常
/// </summary>
namespace Chapter6.Moudle
{
    public class Class1
    {
        public void Test()
        {
            bool catchErrors = true;
            try
            {
                String test = "2147483648";
                int leftHandSide = int.Parse(test);
            }
            catch (FormatException fEx)//捕捉异常
            {
                Console.WriteLine("类型异常。");
            }
            catch (OverflowException oEx)//多个catch处理
            {
                Console.WriteLine("范围超出异常。");
            }
            catch (Exception e) when (catchErrors == true)//筛选异常
            //捕捉全部异常
            //catch 可以省略它的名称
            {
                Console.WriteLine("异常。");
            }
        }
    }
}
 

/// <summary>
/// 6.2使用checked和unchecked进行整数运算
/// </summary>
namespace Chapter6.Moudle
{
    public class Class2
    {
        public void Test()
        {
            //6.2.1编写checked语句
            int number = int.MaxValue;
            checked//任何整数运算溢出都抛出OverflowException异常
            {
                int willThrow1 = number++;
                Console.WriteLine("不会执行!");
            }
            unchecked//强制不检查
            {
                int willThrow1 = number++;
                Console.WriteLine("会执行!");
            }
            //6.2.2编写checked表达式
            int willThrow2 = checked(int.MaxValue - 1);
        }
    }
}
 

/// <summary>
/// 6.3抛出异常
/// </summary>
namespace Chapter6.Moudle
{
    class Class3
    {
        internal void Test()
        {
            throw new NotImplementedException();
        }
    }
}

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值