Exceptions

大作业好烦,烦的我都写不下博客了==。

首先要认识到:

1.Bugs & errors 应该是由程序自身来解决。

2.对于可预测但不可避免的问题使用exception 处理。

关于异常处理的顺序如下

如果在相应方法中找到处理Exception的代码段,

就会调用它对Exception进行处理。

如果没有找到会展开调用栈,到上一层找相应的Exception处理程序。

如果在返回到Main时仍没有找Exception处理程序。

系统CLR会使用DEFAULT Exception处理.

(终止程序/或在debug状态给出”有未被处理的EXCEPTION”)。

日常贴代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
         {
             try
             {
                 Level3();
             }
             catch
             {
                 Console.WriteLine("Main in catch");
             }
         }
 
         static void Level3()
        {
             try
             {
                 Console.WriteLine("Level3 in try");
                 Level2();
             }
             catch
             {
                 Console.WriteLine("Level3 in catch");
                 throw;
             }
             finally
             {
                 Console.WriteLine("Level3 in finally");
             }
         }
 
         static void Level2()
         {
             try
             {
                 Console.WriteLine("Level2 in try");
                 Level1();
             }
             catch
             {
                 Console.WriteLine("Level2 in catch");
                 throw;
             }
             finally
             {
                 Console.WriteLine("Level2 in finally");
             }
         }
 
         static void Level1()
         {
             try
             {
                 Console.WriteLine("Level1 in try");
                 throw new Exception("Level1");
             }
             catch
             {
                 Console.WriteLine("Level1 in catch");
                 throw;
             }
             finally
             {
                  Console.WriteLine("Level1 in finally");
             }
         }
        
    }
}

  

好的,就这样我又写完了一篇博客

posted @ 2015-05-04 21:46  一边挖鼻屎一边  阅读(158)  评论(0编辑  收藏  举报