Program.cs 882 B

123456789101112131415161718192021222324252627282930
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace ConsoleTryCatchFinally
  7. {
  8. internal class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. string str = "明日科技"; //声明一个string类型的变量str
  13. object obj = str; //声明一个object类型的变量obj
  14. try //使用try…catch语句
  15. {
  16. int i = (int)obj; //将obj强制转换成int类型
  17. }
  18. catch (Exception ex) //获取异常
  19. {
  20. Console.WriteLine(ex.Message); //输出异常信息
  21. }
  22. finally //finally语句
  23. {
  24. Console.WriteLine("程序执行完毕..."); //输出“程序执行完毕…”
  25. }
  26. Console.ReadLine();
  27. }
  28. }
  29. }