异常处理

using System;
using System.Collections.Generic;
using System.Xml.Serialization;

class Program
{
    static void Main(string[] args)
    {
        Fa().Write();
        Fa().Write();
        Fa().Write();
    }

    private static int result = 0;
    static int Fa()
    {
        try
        {
            result++;

            return result;
            //throw new Exception("aaaaaaaaa");
        }
        catch (Exception)
        {
            result++;
        }
        finally
        {
            result++;
        }
        return result;
    }
}









static class ObjectExt
{
    public static void Write(this object obj)
    {
        Console.WriteLine(obj);
    }

    public static void Write<T>(this IEnumerable<T> obj)
    {
        if (obj is string)
        {
            Console.WriteLine(obj);
            return;
        }
        foreach (var o in obj)
        {
            Console.WriteLine(o);
        }
    }
}

2.

using System;
using System.Collections.Generic;
using System.Xml.Serialization;

class Person
{
    public int Age { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
        Fa().Age.Write();
        Fa().Age.Write();
        Fa().Age.Write();
    }


    static Person Fa()
    {
        var p = new Person
        {
            Age = 100
        };
        try
        {
            p.Age++;
            //throw new Exception("");
            return p;
            //throw new Exception("aaaaaaaaa");
        }
        catch (Exception)
        {
            p.Age++;
            return p;
        }
        finally
        {
            p.Age++;
        }
    }
}









static class ObjectExt
{
    public static void Write(this object obj)
    {
        Console.WriteLine(obj);
    }

    public static void Write<T>(this IEnumerable<T> obj)
    {
        if (obj is string)
        {
            Console.WriteLine(obj);
            return;
        }
        foreach (var o in obj)
        {
            Console.WriteLine(o);
        }
    }
}
原文地址:https://www.cnblogs.com/zhaoxianglong1987/p/7686147.html