异常--try..catch
class Program { static void Main(string[] args) { try { object obj = null; int N = (int)obj; } catch(Exception ex) { Console.WriteLine("捕获异常:" + ex); } Console.ReadLine(); } }
try...catch...finally
class Program { static void Main(string[] args) { try { object obj = null; int N = (int)obj; } catch(Exception ex) { Console.WriteLine("捕获异常:" + ex); } finally//最后执行的语句 { Console.WriteLine("\n程序执行完毕..."); } Console.ReadLine(); } }