• C#异常处理。


    一、什么是异常?

      程序运行时发生的错误。

    二、异常处理的一般代码模式。

      try{..可能发生异常的代码} catch{..对异常的处理} finally{...无论是否发生异常、是否捕获异常都会执行的代码}。

    实例:

    简单实例:

    try中某句代码异常后,后面的代码不会执行,直接跳转到catch

    finally的使用:希望代码无论如何都要执行,就把代码放finally中  

          1、catch无法捕获异常,程序崩溃,但会在崩溃前执行finally 中的代码。

          2、如果catch里发生了异常。。。。。。

          3、catch中有return时,也会在return前执行。

    class Program
        {
            static void Main(string[] args)
            {
                Person person = new Person();
                person = null;
                try
                {
                    person.Name = "张三";
                }
                catch (Exception ex)
                {
    
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    Console.WriteLine("$$$$$$$$$$");
                }
            }
        }
        public class Person
        {
            public string Name { get; set; }
        }
  • 相关阅读:
    Codeforces 424C(异或)
    CodeForces
    Codeforces 424A (思维题)
    HDU 1197 Specialized Four-Digit Numbers
    ZOJ 2301 Color the Ball 线段树(区间更新+离散化)
    HDU 1106 排序
    Codefroces 831B Keyboard Layouts
    POJ 1082 Calendar Game
    HDU 多校联合 6045
    HDU 5976 Detachment
  • 原文地址:https://www.cnblogs.com/zhangyuhao/p/10484831.html
Copyright © 2020-2023  润新知