Program.cs 667 B

12345678910111213141516171819202122232425
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace ConsoleTryCatch
  7. {
  8. internal class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. try //使用try…catch语句
  13. {
  14. object obj = null; //声明一个object变量,初始值为null
  15. int N = (int)obj; //将object类型强制转换成int类型
  16. }
  17. catch (Exception ex) //捕获异常
  18. {
  19. Console.WriteLine("捕获异常:" + ex); //输出异常
  20. }
  21. Console.ReadLine();
  22. }
  23. }
  24. }