2015-04-07 63 views

回答

1

這裏的區別是:請詢問清楚的認識例如

例子:

class Program 
    { 
    static void Main() 
    { 
     try 
     { 
      int[] array = new int[100]; 
      array[0] = 1; 
      array[10] = 2; 
      array[200] = 3; // this line will through *IndexOutOfRangeException* Exception 

      object o = null; 
      o.ToString(); // this line will through *NullReferenceException* Exception 
     } 
     /* the below catch block(IndexOutOfRangeException class) will only catch *IndexOutOfRangeException* and not *NullReferenceException* 
      hence you can say it as Specific Exception as it is catching only a particular exception. 
     */ 
     catch (IndexOutOfRangeException e) 
     { 
      Console.WriteLine("Incorrect Index"); // Or any of you Custom error message etc. 
     } 
     /* the below catch block(Exception class) will catch all the type of exception and hence you can call it as Generic Exception. 
     */ 
     catch (Exception e) 
     { 
      Console.WriteLine("Opps Something Went Wrong..."); // Or any of you Custom error message etc. 
     } 

    } 
} 

特例:正如你在上面IndexOutOfRange看到的例子僅處理一個類型的異常因此你可以把它作爲特定的例外。

通用異常:這些Exception類可以處理任何類型的異常。所以可以把它稱爲泛化例外。

你可以得到更多的信息here。對於例外的層次結構,您可以看看here

+0

謝謝。這很好解釋。 – Sunny

+0

@孫尼我的榮幸。你能把它標記爲答案嗎? –