1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Test10 7 { 8 class PRogram 9 {10 //int a = 0; 注意存放位置?。。。?!11 const int i = 927;12 static void Main(string[] args)13 {14 int a = 0;15 try16 {17 //i = 112;18 a = i / a;19 Console.WriteLine(a);20 }21 catch (Exception e)22 {23 Console.WriteLine(e.Message);24 Console.ReadLine();25 }26 finally27 {28 System.Console.WriteLine("無論是否發生異常,我都會顯示。");29 } 30 }31 }32 }
在第10行那里因為自己開始定義了a,導致“非靜態的字段、方法或屬性“Test10.Program.a”要求對象引用”錯誤出現,將變量聲明放到 Main方法里自然就好了。
另外,catch (Exception e),最后的e可以用任何ex等表示,只要ex.Message保持對應即可。
剛開始接觸,可能感覺啥都新鮮,還請諸位知道,謝謝!
新聞熱點
疑難解答