開発環境
- Microsoft Windows 7 Home Premium (OS)
- Microsoft Visual C# 2010 Express Edition (IDE)
- 言語: C#
独習C# 第3版 ハーバート・シルト (著) エディフィストラーニング株式会社 矢嶋聡 (監修, 翻訳) の第10章(C例外処理)の理解度チェック8を解いてみる。
8.
スタックが空の状態を通知するカスタム例外をテスト。
コード
using System; public class FullException : Exception { public FullException() : base() { } public FullException(string str) : base(str) { } public FullException(string str, Exception inner) : base(str, inner) { } public FullException( System.Runtime.Serialization.SerializationInfo si, System.Runtime.Serialization.StreamingContext sc) : base(si, sc) { } } public class EmptyException : Exception { public EmptyException() : base() { } public EmptyException(string str) : base(str) { } public EmptyException(string str, Exception inner) : base(str, inner) { } public EmptyException( System.Runtime.Serialization.SerializationInfo si, System.Runtime.Serialization.StreamingContext sc) : base(si, sc) { } public override string ToString() { return Message; } } public class Stack { private char[] s; private int loc; public Stack(int size) { s = new char[size]; loc = 0; } public Stack(Stack obj) { loc = obj.loc; s = new char[obj.s.Length]; for (int i = 0; i < s.Length; i++) { s[i] = obj.s[i]; } } public Stack(char[] a) { loc = 0; s = new char[a.Length + 1]; for (int i = 0; i < a.Length; i++) { try { Push(a[i]); } catch (FullException exc) { Console.WriteLine(exc); } } } public void Push(char ch) { if (loc == s.Length) { throw new FullException(); } s[loc] = ch; loc++; } public char Pop() { if (loc == 0) { throw new EmptyException(); } loc--; return s[loc]; } } class Tester { public void Run() { Stack s = new Stack(10); for (int i = 0; i < 10; i++) { s.Push((char)('A' + i)); } for (int i = 0; i < 11; i++) { try { char ch = s.Pop(); Console.WriteLine(ch); } catch (EmptyException exc) { Console.WriteLine(exc); } } } static void Main() { Tester t = new Tester(); t.Run(); } }
入出力結果(Console Window)
J I H G F E D C B A 種類 'EmptyException' の例外がスローされました。 続行するには何かキーを押してください . . .
スタックがいっぱいの状態を通知するカスタム例外をテスト。
コード
using System; public class FullException : Exception { public FullException() : base() { } public FullException(string str) : base(str) { } public FullException(string str, Exception inner) : base(str, inner) { } public FullException( System.Runtime.Serialization.SerializationInfo si, System.Runtime.Serialization.StreamingContext sc) : base(si, sc) { } } public class EmptyException : Exception { public EmptyException() : base() { } public EmptyException(string str) : base(str) { } public EmptyException(string str, Exception inner) : base(str, inner) { } public EmptyException( System.Runtime.Serialization.SerializationInfo si, System.Runtime.Serialization.StreamingContext sc) : base(si, sc) { } public override string ToString() { return Message; } } public class Stack { private char[] s; private int loc; public Stack(int size) { s = new char[size]; loc = 0; } public Stack(Stack obj) { loc = obj.loc; s = new char[obj.s.Length]; for (int i = 0; i < s.Length; i++) { s[i] = obj.s[i]; } } public Stack(char[] a) { loc = 0; s = new char[a.Length + 1]; for (int i = 0; i < a.Length; i++) { try { Push(a[i]); } catch (FullException exc) { Console.WriteLine(exc); } } } public void Push(char ch) { if (loc == s.Length) { throw new FullException(); } s[loc] = ch; loc++; } public char Pop() { if (loc == 0) { throw new EmptyException(); } loc--; return s[loc]; } } class Tester { public void Run() { try { Stack s = new Stack(0); s.Push('a'); Console.WriteLine(s.Pop()); } catch (FullException exc) { Console.WriteLine(exc); } } static void Main() { Tester t = new Tester(); t.Run(); } }
入出力結果(Console Window)
FullException: 種類 'FullException' の例外がスローされました。 続行するには何かキーを押してください . . .
0 コメント:
コメントを投稿