4章 例外処理
・引数のないcatchブロックは使いべきではない。・階層が浅い所にいる関数でtry- catch or try - catch - finallyを行う。
・階層が深いところでは、リソースの解放とかが必要な場合に、try - finallyを行う。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// youtube
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.YouTube;
using Google.GData.Extensions.MediaRss;
using Google.YouTube;
namespace YoutubeTest001
{
class Program
{
static void Main(string[] args)
{
string developerKey = "作成したデベロッパーキー";
YouTubeRequestSettings settings = new YouTubeRequestSettings("example app", developerKey);
YouTubeRequest request = new YouTubeRequest(settings);
Uri videoEntryUrl = new Uri("http://gdata.youtube.com/feeds/api/videos/ADos_xW4_J0");
Video video = request.Retrieve
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// youtube
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.YouTube;
using Google.GData.Extensions.MediaRss;
using Google.YouTube;
namespace YoutubeTest001
{
class Program
{
static void Main(string[] args)
{
}
}
}
①psi.RedirectStandardError = true; ②string error = p.StandardError.ReadToEnd(); ③Console.WriteLine(error);
class Program
{
static void Main(string[] args)
{
Kacho kousaku = new Kacho();
kousaku.Work();
Console.ReadLine();
}
}
class Kacho
{
// 部下を加える
Hira dameo = new Hira();
//(1)イベントハンドラ追加 (発行する人が定義する)
public event EventHandler DenwaEnd; // 電話が終わった
//(2)イベント実行用の関数を定義 (発行する人が定義する)
private void OnDenwaEnd(EventArgs e)
{
if (DenwaEnd != null)
{
DenwaEnd(this, e);
}
}
public Kacho()
{
//(6)だめおにイベントを受け取らせるための準備
dameo.Subscribe(this);
}
public void Work()
{
//課長仕事開始
for (int i = 0; i < 10; i++)
{
Console.WriteLine("課長電話中。");
System.Threading.Thread.Sleep(1000);
}
//(3)イベントを発生させる。
OnDenwaEnd(EventArgs.Empty);
}
}
class Hira
{
// (4)イベントを受け取る設定をする。
// 受け取るイベントと受け取ったときに呼ばれる関数をひもづける
public void Subscribe(Kacho kacho)
{
kacho.DenwaEnd += KachoDenwaEnd;
}
// (5)イベント発生時に実施する処理を定義
public void KachoDenwaEnd(object sender, EventArgs e)
{
Console.WriteLine("課長。例の件なのですが、AAAなので、BBBにします。よろしいでしょうか?");
}
}