開発環境
- OS: Windows 10 Pro
- IDE(統合開発環境): Visual Studio Community 2017
Head First C# ―頭とからだで覚えるC#の基本 (Andrew Stellman (著)、Jennifer Greene (著)、佐藤 嘉一 (監修, 監修)、木下 哲也 (翻訳)、オライリージャパン)の8章(例外処理 - 消化は時代遅れ)、プールパズル(p. 359)を取り組んでみる。
コード
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.IO;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Kangaroo joey = new Kangaroo();
int koala = joey.wombat(joey.wombat(joey.wombat(1)));
try
{
Console.WriteLine((15 / koala) + " 個の卵");
}
catch (DivideByZeroException)
{
Console.WriteLine("こんにちは!");
}
}
}
public class Kangaroo
{
FileStream fs;
int croc;
int dingo = 0;
public int wombat(int wallaby)
{
dingo++;
try
{
if (wallaby > 0)
{
fs = File.OpenWrite("wobbiegong");
croc = 0;
}
else if (wallaby < 0)
{
croc = 3;
}
else
{
fs = File.OpenRead("wobbiegong");
croc = 1;
}
}
catch (IOException)
{
croc = -3;
}
catch
{
croc = 4;
}
finally
{
if (dingo > 2)
{
croc -= dingo;
}
}
return croc;
}
}
}
入出力結果(コマンドプロンプト)
こんにちは! 続行するには何かキーを押してください . . .
0 コメント:
コメントを投稿