Try、Catch、Finally

来源:互联网 发布:云计算股票代码 编辑:程序博客网 时间:2024/04/26 18:24

初步:

        static void Main(string[] args)        {            Console.WriteLine("请输入一个数字:");            int num = 0;            bool b = true;            try            {                num = Convert.ToInt32(Console.ReadLine());                //如果上面发生异常,则下述语句均不执行,直接跳到catch块中                Console.WriteLine("1111");                Console.WriteLine("2222");                Console.WriteLine("3333");            }                //try与catch之间不允许添加任何语句            catch            {                Console.WriteLine("输入的数字有误,程序退出。");                b = false;            }            if(b)            {                Console.WriteLine("输入的数字是:{0}", num);            }            Console.ReadKey();        }

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace TryCatchFinallyPractise{    class Program    {        static string[] eType = { "none", "simple", "index", "nested index" };        static void Main(string[] args)        {            foreach (var item in eType)            {                try                {                    Console.WriteLine("Main() try block reached.");                    Console.WriteLine("ThrowException (\"{0}\")",item);                    ThrowException(item);                    Console.WriteLine("Main() try block continues.");                }                catch (System.IndexOutOfRangeException e)                {                    Console.WriteLine("Main() System.IndexOutOfRangeException catch" +                        "block reached. Message:\n\"{0}\"", e.Message);                }                catch //可以相应所有异常(如果没有被相应的catch块处理的话)                {                    Console.WriteLine("Main() general catch block reached.");                }                finally                 {                    Console.WriteLine("Main() finally block reached.");                }                Console.WriteLine();            }            Console.ReadKey();        }        static void ThrowException(string str)        {            Console.WriteLine("ThrowException (\"{0}\") reached", str);            switch (str)            {                case "none":                    Console.WriteLine("Not throwing an exception.");                    break;                case "simple":                    Console.WriteLine("Throwing System.Exception.");                    throw new SystemException(); //使用throw关键字生成一个异常                    //break;   //不需要break,使用throw就可以结束该块的执行                case "index":                    Console.WriteLine("Throwing System.IndexOutOfRangeExcetpion.");                    eType[4] = "index error";                    break;                case "nested index":                    try                    {                        Console.WriteLine("ThrowException (\"nested index\")"                            + "try block reached.");                        Console.WriteLine("ThrowException(\"index\") called");                        ThrowException("index"); //发生的异常由自己的catch块来处理,然后执行自己的finally                     }                    catch //可以相应所有异常(如果没有被相应的catch块处理的话)                    {                        Console.WriteLine("ThrowException(\"nested index\") general" +                            "catch block reached.");                    }                    finally                    {                        Console.WriteLine("ThrowException(\"nested index\") finally" +                            "block reached");                    }                    break;            }        }    }}


注意:必须在更一般的异常捕获之前为比较特殊的异常提供catch块,如果catch块的顺序有误,应用程序就会编译失败,还要注意可以在catch中抛出异常,方法是使用throw;,这个表达式会在此抛出catch块处理过的异常,但该异常就不会由当前的try...catch...finally处理,而是由上一级的代码处理(但是嵌套结构中的finally块仍会执行):

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace TryCatchFinallyPractise{    class Program    {        static string[] eType = { "none", "simple", "index", "nested index" };        static void Main(string[] args)        {            foreach (var item in eType)            {                try                {                    Console.WriteLine("Main() try block reached.");                    Console.WriteLine("ThrowException (\"{0}\")",item);                    ThrowException(item);                    Console.WriteLine("Main() try block continues.");                }                catch (System.IndexOutOfRangeException e)                {                    Console.WriteLine("Main() System.IndexOutOfRangeException catch" +                        "block reached. Message:\n\"{0}\"", e.Message);                }                catch //可以相应所有异常(如果没有被相应的catch块处理的话)                {                    Console.WriteLine("Main() general catch block reached.");                }                finally                 {                    Console.WriteLine("Main() finally block reached.");                }                Console.WriteLine();            }            Console.ReadKey();        }        static void ThrowException(string str)        {            Console.WriteLine("ThrowException (\"{0}\") reached", str);            switch (str)            {                case "none":                    Console.WriteLine("Not throwing an exception.");                    break;                case "simple":                    Console.WriteLine("Throwing System.Exception.");                    throw new SystemException(); //使用throw关键字生成一个异常                    //break;   //不需要break,使用throw就可以结束该块的执行                case "index":                    Console.WriteLine("Throwing System.IndexOutOfRangeExcetpion.");                    eType[4] = "index error";                    break;                case "nested index":                    try                    {                        Console.WriteLine("ThrowException (\"nested index\")"                            + "try block reached.");                        Console.WriteLine("ThrowException(\"index\") called");                        ThrowException("index"); //发生的异常由自己的catch块来处理,然后执行自己的finally                                             }                    catch //可以相应所有异常(如果没有被相应的catch块处理的话)                    {                        Console.WriteLine("ThrowException(\"nested index\") general" +                            "catch block reached.");                        throw;//指定由上一级代码处理异常                    }                    finally                    {                        Console.WriteLine("ThrowException(\"nested index\") finally" +                            "block reached");                    }                    break;            }        }    }}


0 0