catch、finallyのサンプル

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace ConsoleApplication1
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Console.WriteLine("処理開始");
  13.             String str = "a";
  14.             try
  15.             {
  16.                 Console.WriteLine("キャスト開始");
  17.                 int tmp = Int32.Parse(str);
  18.                 Console.WriteLine("ここは実行されない");
  19.             }
  20.             catch (FormatException ex)
  21.             {
  22.                 Console.WriteLine("catch");
  23.             }
  24.             finally
  25.             {
  26.                 Console.WriteLine("finally");
  27.             }
  28.             Console.WriteLine("処理終了");
  29.             Console.ReadLine();
  30.         }
  31.     }
  32. }

image

実行ファイルダウンロード

戻る