この記事では、この API のリファレンス ドキュメントに補足的な解説を提供します。
Exception クラスは、すべての例外の基底クラスです。 エラーが発生すると、システムまたは現在実行中のアプリケーションのいずれかが、エラーに関する情報を含む例外をスローして報告します。 例外がスローされた後、その例外はアプリケーションまたは既定の例外ハンドラーによって処理されます。
エラーと例外
実行時エラーは、さまざまな理由で発生する可能性があります。 ただし、すべてのエラーをコードの例外として処理する必要はありません。 実行時に発生する可能性があるエラーのカテゴリと、それらに対応する適切な方法を次に示します。
使用の誤り。 使用エラーは、例外が発生する可能性があるプログラム ロジックのエラーを表します。 ただし、エラーに対処する場合は、例外処理ではなく、問題のあるコードを変更する必要があります。 たとえば、次の例の Object.Equals(Object) メソッドのオーバーライドでは、
obj
引数は常に null 以外である必要があることを前提としています。using System; public class Person1 { private string _name; public string Name { get { return _name; } set { _name = value; } } public override int GetHashCode() { return this.Name.GetHashCode(); } public override bool Equals(object obj) { // This implementation contains an error in program logic: // It assumes that the obj argument is not null. Person1 p = (Person1) obj; return this.Name.Equals(p.Name); } } public class UsageErrorsEx1 { public static void Main() { Person1 p1 = new Person1(); p1.Name = "John"; Person1 p2 = null; // The following throws a NullReferenceException. Console.WriteLine($"p1 = p2: {p1.Equals(p2)}"); } }
// In F#, null is not a valid state for declared types // without 'AllowNullLiteralAttribute' [<AllowNullLiteral>] type Person() = member val Name = "" with get, set override this.GetHashCode() = this.Name.GetHashCode() override this.Equals(obj) = // This implementation contains an error in program logic: // It assumes that the obj argument is not null. let p = obj :?> Person this.Name.Equals p.Name let p1 = Person() p1.Name <- "John" let p2: Person = null // The following throws a NullReferenceException. printfn $"p1 = p2: {p1.Equals p2}"
Public Class Person Private _name As String Public Property Name As String Get Return _name End Get Set _name = value End Set End Property Public Overrides Function Equals(obj As Object) As Boolean ' This implementation contains an error in program logic: ' It assumes that the obj argument is not null. Dim p As Person = CType(obj, Person) Return Me.Name.Equals(p.Name) End Function End Class Module Example2 Public Sub Main() Dim p1 As New Person() p1.Name = "John" Dim p2 As Person = Nothing ' The following throws a NullReferenceException. Console.WriteLine("p1 = p2: {0}", p1.Equals(p2)) End Sub End Module
NullReferenceExceptionが
obj
されたときに発生するnull
例外は、Object.Equalsオーバーライドを呼び出してから再コンパイルする前に、ソース コードを変更して null を明示的にテストすることで排除できます。 次の例には、null
引数を処理する修正されたソース コードが含まれています。using System; public class Person2 { private string _name; public string Name { get { return _name; } set { _name = value; } } public override int GetHashCode() { return this.Name.GetHashCode(); } public override bool Equals(object obj) { // This implementation handles a null obj argument. Person2 p = obj as Person2; if (p == null) return false; else return this.Name.Equals(p.Name); } } public class UsageErrorsEx2 { public static void Main() { Person2 p1 = new Person2(); p1.Name = "John"; Person2 p2 = null; Console.WriteLine($"p1 = p2: {p1.Equals(p2)}"); } } // The example displays the following output: // p1 = p2: False
// In F#, null is not a valid state for declared types // without 'AllowNullLiteralAttribute' [<AllowNullLiteral>] type Person() = member val Name = "" with get, set override this.GetHashCode() = this.Name.GetHashCode() override this.Equals(obj) = // This implementation handles a null obj argument. match obj with | :? Person as p -> this.Name.Equals p.Name | _ -> false let p1 = Person() p1.Name <- "John" let p2: Person = null printfn $"p1 = p2: {p1.Equals p2}" // The example displays the following output: // p1 = p2: False
Public Class Person2 Private _name As String Public Property Name As String Get Return _name End Get Set _name = Value End Set End Property Public Overrides Function Equals(obj As Object) As Boolean ' This implementation handles a null obj argument. Dim p As Person2 = TryCast(obj, Person2) If p Is Nothing Then Return False Else Return Me.Name.Equals(p.Name) End If End Function End Class Module Example3 Public Sub Main() Dim p1 As New Person2() p1.Name = "John" Dim p2 As Person2 = Nothing Console.WriteLine("p1 = p2: {0}", p1.Equals(p2)) End Sub End Module ' The example displays the following output: ' p1 = p2: False
使用エラーに例外処理を使用する代わりに、 Debug.Assert メソッドを使用してデバッグ ビルドの使用状況エラーを特定し、 Trace.Assert メソッドを使用してデバッグ ビルドとリリース ビルドの両方で使用エラーを識別できます。 詳細については、「 マネージド コードのアサーション」を参照してください。
プログラム エラー。 プログラム エラーは、バグのないコードを記述することによって必ずしも回避できない実行時エラーです。
場合によっては、プログラム エラーに予期されるエラーまたはルーチン エラー状態が反映される場合があります。 この場合は、例外処理を使用してプログラム エラーに対処しないようにし、代わりに操作を再試行することをお勧めします。 たとえば、ユーザーが特定の形式で日付を入力することが予想される場合は、DateTime.TryParseExact メソッドを呼び出して日付文字列を解析できます。このメソッドは、Boolean メソッドを使用するのではなく、解析操作が成功したかどうかを示すDateTime.ParseExact値を返します。このメソッドは、日付文字列をFormatException値に変換できない場合にDateTime例外をスローします。 同様に、ユーザーが存在しないファイルを開こうとした場合は、最初に File.Exists メソッドを呼び出してファイルが存在するかどうかを確認し、存在しない場合は、ファイルを作成するかどうかをユーザーに求めることができます。
それ以外の場合、プログラム エラーには、コードで処理できる予期しないエラー状態が反映されます。 たとえば、ファイルが存在することを確認するためにチェックした場合でも、ファイルを開く前に削除されたり、破損したりする可能性があります。 その場合、 StreamReader オブジェクトをインスタンス化するか、 Open メソッドを呼び出してファイルを開こうとすると、 FileNotFoundException 例外がスローされることがあります。 このような場合は、例外処理を使用してエラーから回復する必要があります。
システム障害。 システム 障害は実行時エラーであり、プログラムで意味のある方法で処理することはできません。 たとえば、共通言語ランタイムが追加のメモリを割り当てることができない場合、メソッドは OutOfMemoryException 例外をスローできます。 通常、システム障害は例外処理を使用して処理されません。 代わりに、 AppDomain.UnhandledException などのイベントを使用し、 Environment.FailFast メソッドを呼び出して例外情報をログに記録し、アプリケーションが終了する前にエラーをユーザーに通知することができます。
Try/Catch ブロック
共通言語ランタイムは、例外をオブジェクトとして表現し、プログラム コードと例外処理コードを try
ブロックと catch
ブロックに分離した例外処理モデルを提供します。 1 つ以上の catch
ブロックがあり、それぞれが特定の種類の例外を処理するように設計されているか、別のブロックよりも具体的な例外をキャッチするように設計された 1 つのブロックがあります。
アプリケーション コードのブロックの実行中に発生する例外をアプリケーションが処理する場合、コードは try
ステートメント内に配置する必要があり、 try
ブロックと呼ばれます。
try
ブロックによってスローされる例外を処理するアプリケーション コードは、catch
ステートメント内に配置され、catch
ブロックと呼ばれます。 0 個以上の catch
ブロックは try
ブロックに関連付けられます。各 catch
ブロックには、処理する例外の種類を決定する型フィルターが含まれています。
try
ブロックで例外が発生した場合、システムは、例外を処理するcatch
ブロックが見つかるまで、関連付けられているcatch
ブロックをアプリケーション コードに表示される順序で検索します。
catch
ブロックは、catch ブロックの型フィルターがT
またはそれから派生する任意の型を指定した場合、T
型の例外を処理します。 例外を処理する最初の catch
ブロックが見つかると、システムは検索を停止します。 このため、アプリケーション コードでは、このセクションの後の例で示すように、型を処理する catch
ブロックを、その基本型を処理する catch
ブロックの前に指定する必要があります。
System.Exception
を処理する catch ブロックは最後に指定されます。
現在のcatch
ブロックに関連付けられているtry
ブロックで例外が処理されておらず、現在のtry
ブロックが現在の呼び出しの他のtry
ブロック内に入れ子になっている場合は、次に囲むcatch
ブロックに関連付けられているtry
ブロックが検索されます。 例外の catch
ブロックが見つからない場合、システムは現在の呼び出しで以前の入れ子レベルを検索します。 現在の呼び出しで例外の catch
ブロックが見つからない場合、例外は呼び出し履歴に渡され、前のスタック フレームは例外を処理する catch
ブロックを検索します。 呼び出し履歴の検索は、例外が処理されるまで、または呼び出し履歴にこれ以上フレームが存在しないまで続行されます。 例外を処理する catch
ブロックが見つからずに呼び出し履歴の先頭に到達すると、既定の例外ハンドラーによって処理され、アプリケーションが終了します。
F# の try..with 式
F# では、 catch
ブロックは使用されません。 代わりに、発生した例外は、単一のwith
ブロックを使用してパターンマッチされます。 これはステートメントではなく式であるため、すべてのパスは同じ型を返す必要があります。 詳細については、try...with 式を参照してください。
例外タイプの特徴
例外の種類では、次の機能がサポートされています。
エラーを説明する人間が判読できるテキスト。 例外が発生すると、ランタイムは、エラーの性質をユーザーに通知し、問題を解決するためのアクションを提案するテキスト メッセージを使用できるようにします。 このテキスト メッセージは、例外オブジェクトの Message プロパティに保持されます。 例外オブジェクトの作成時に、テキスト文字列をコンストラクターに渡して、その特定の例外の詳細を記述できます。 コンストラクターにエラー メッセージ引数が指定されていない場合は、既定のエラー メッセージが使用されます。 詳細については、 Message プロパティを参照してください。
例外がスローされたときの呼び出しスタックの状態。 StackTrace プロパティには、コードでエラーが発生する場所を特定するために使用できるスタック トレースが含まれています。 スタック トレースには、呼び出されたすべてのメソッドと、呼び出しが行われるソース ファイル内の行番号が一覧表示されます。
例外クラスのプロパティ
Exception クラスには、コードの場所、型、ヘルプ ファイル、例外の理由を識別するのに役立つプロパティが多数含まれています。StackTrace、InnerException、Message、HelpLink、HResult、Source、TargetSite、およびData。
2 つ以上の例外の間に因果関係が存在する場合、 InnerException プロパティはこの情報を保持します。 この内部例外に応答して、外部例外がスローされます。 外側の例外を処理するコードでは、以前の内部例外の情報を使用して、エラーをより適切に処理できます。 例外に関する補足情報は、キーと値のペアのコレクションとして Data プロパティに格納できます。
例外オブジェクトの作成時にコンストラクターに渡されるエラー メッセージ文字列はローカライズする必要があり、 ResourceManager クラスを使用してリソース ファイルから指定できます。 ローカライズされたリソースの詳細については、「 サテライト アセンブリの作成 と リソースのパッケージ化とデプロイ 」トピックを参照してください。
例外が発生した理由に関する詳細情報をユーザーに提供するために、 HelpLink プロパティはヘルプ ファイルへの URL (または URN) を保持できます。
Exception クラスは、値が0x80131500を持つ HRESULT COR_E_EXCEPTION
を使用します。
Exception クラスのインスタンスの初期プロパティ値の一覧については、Exception コンストラクターを参照してください。
パフォーマンスに関する考慮事項
例外をスローまたは処理すると、大量のシステム リソースと実行時間が消費されます。 例外は、予測可能なイベントやフロー制御を処理するためではなく、本当に異常状態を処理するために限ります。 たとえば、クラス ライブラリを開発する場合など、メソッド引数が無効な場合は例外をスローするのが妥当な場合があります。これは、メソッドが有効なパラメーターで呼び出されることを想定しているためです。 無効なメソッド引数が使用エラーの結果でない場合は、異常なことが発生したことを意味します。 逆に、ユーザー入力が無効な場合は例外をスローしないでください。これは、ユーザーが無効なデータを入力する場合があるためです。 代わりに、ユーザーが有効な入力を入力できるように再試行メカニズムを指定します。 また、使用エラーを処理するために例外を使用する必要もありません。 代わりに、 アサーションを 使用して、使用エラーを特定して修正します。
さらに、リターンコードで十分な場合は例外をスローしないでください。戻りコードを例外に変換しないでください。そして、例外を定期的にキャッチして無視した後に処理を続行することはしないでください。
例外の再スロー
多くの場合、例外ハンドラーは単に例外を呼び出し元に渡す必要があります。 これは、ほとんどの場合、次の場合に発生します。
.NET クラス ライブラリまたはその他のクラス ライブラリ内のメソッドの呼び出しをラップするクラス ライブラリ。
致命的な例外が発生したアプリケーションまたはライブラリ。 例外ハンドラーは、例外をログに記録してから、例外を再スローできます。
例外を再スローする推奨される方法は、C# の throw ステートメント、F# の reraise 関数、および Visual Basic の Throw ステートメントを式を含めずに使用することです。 これにより、例外が呼び出し元に伝達されるときに、すべての呼び出し履歴情報が保持されます。 この例を次に示します。 文字列拡張メソッド ( FindOccurrences
) は、引数を事前に検証せずに、 String.IndexOf(String, Int32) への 1 つ以上の呼び出しをラップします。
using System;
using System.Collections.Generic;
public static class Library1
{
public static int[] FindOccurrences(this String s, String f)
{
var indexes = new List<int>();
int currentIndex = 0;
try
{
while (currentIndex >= 0 && currentIndex < s.Length)
{
currentIndex = s.IndexOf(f, currentIndex);
if (currentIndex >= 0)
{
indexes.Add(currentIndex);
currentIndex++;
}
}
}
catch (ArgumentNullException)
{
// Perform some action here, such as logging this exception.
throw;
}
return indexes.ToArray();
}
}
open System
module Library =
let findOccurrences (s: string) (f: string) =
let indexes = ResizeArray()
let mutable currentIndex = 0
try
while currentIndex >= 0 && currentIndex < s.Length do
currentIndex <- s.IndexOf(f, currentIndex)
if currentIndex >= 0 then
indexes.Add currentIndex
currentIndex <- currentIndex + 1
with :? ArgumentNullException ->
// Perform some action here, such as logging this exception.
reraise ()
indexes.ToArray()
Imports System.Collections.Generic
Imports System.Runtime.CompilerServices
Public Module Library
<Extension()>
Public Function FindOccurrences1(s As String, f As String) As Integer()
Dim indexes As New List(Of Integer)
Dim currentIndex As Integer = 0
Try
Do While currentIndex >= 0 And currentIndex < s.Length
currentIndex = s.IndexOf(f, currentIndex)
If currentIndex >= 0 Then
indexes.Add(currentIndex)
currentIndex += 1
End If
Loop
Catch e As ArgumentNullException
' Perform some action here, such as logging this exception.
Throw
End Try
Return indexes.ToArray()
End Function
End Module
呼び出し元は、 FindOccurrences
を 2 回呼び出します。
FindOccurrences
の 2 番目の呼び出しでは、呼び出し元は検索文字列としてnull
を渡します。これにより、String.IndexOf(String, Int32) メソッドはArgumentNullException例外をスローします。 この例外は、 FindOccurrences
メソッドによって処理され、呼び出し元に返されます。 throw ステートメントは式なしで使用されるため、この例からの出力は呼び出し履歴が保持されていることを示しています。
public class RethrowEx1
{
public static void Main()
{
String s = "It was a cold day when...";
int[] indexes = s.FindOccurrences("a");
ShowOccurrences(s, "a", indexes);
Console.WriteLine();
String toFind = null;
try
{
indexes = s.FindOccurrences(toFind);
ShowOccurrences(s, toFind, indexes);
}
catch (ArgumentNullException e)
{
Console.WriteLine($"An exception ({e.GetType().Name}) occurred.");
Console.WriteLine($"Message:{Environment.NewLine} {e.Message}{Environment.NewLine}");
Console.WriteLine($"Stack Trace:{Environment.NewLine} {e.StackTrace}{Environment.NewLine}");
}
}
private static void ShowOccurrences(String s, String toFind, int[] indexes)
{
Console.Write("'{0}' occurs at the following character positions: ",
toFind);
for (int ctr = 0; ctr < indexes.Length; ctr++)
Console.Write("{0}{1}", indexes[ctr],
ctr == indexes.Length - 1 ? "" : ", ");
Console.WriteLine();
}
}
// The example displays the following output:
// 'a' occurs at the following character positions: 4, 7, 15
//
// An exception (ArgumentNullException) occurred.
// Message:
// Value cannot be null.
// Parameter name: value
//
// Stack Trace:
// at System.String.IndexOf(String value, Int32 startIndex, Int32 count, Stri
// ngComparison comparisonType)
// at Library.FindOccurrences(String s, String f)
// at Example.Main()
open Library
let showOccurrences toFind (indexes: int[]) =
printf $"'{toFind}' occurs at the following character positions: "
for i = 0 to indexes.Length - 1 do
printf $"""{indexes[i]}{if i = indexes.Length - 1 then "" else ", "}"""
printfn ""
let s = "It was a cold day when..."
let indexes = findOccurrences s "a"
showOccurrences "a" indexes
printfn ""
let toFind: string = null
try
let indexes = findOccurrences s toFind
showOccurrences toFind indexes
with :? ArgumentNullException as e ->
printfn $"An exception ({e.GetType().Name}) occurred."
printfn $"Message:\n {e.Message}\n"
printfn $"Stack Trace:\n {e.StackTrace}\n"
// The example displays the following output:
// 'a' occurs at the following character positions: 4, 7, 15
//
// An exception (ArgumentNullException) occurred.
// Message:
// Value cannot be null. (Parameter 'value')
//
// Stack Trace:
// at System.String.IndexOf(String value, Int32 startIndex, Int32 count, Stri
// ngComparison comparisonType)
// at Library.findOccurrences(String s, String f)
// at <StartupCode$fs>.main@()
Module Example1
Public Sub Main()
Dim s As String = "It was a cold day when..."
Dim indexes() As Integer = s.FindOccurrences1("a")
ShowOccurrences(s, "a", indexes)
Console.WriteLine()
Dim toFind As String = Nothing
Try
indexes = s.FindOccurrences1(toFind)
ShowOccurrences(s, toFind, indexes)
Catch e As ArgumentNullException
Console.WriteLine("An exception ({0}) occurred.",
e.GetType().Name)
Console.WriteLine("Message:{0} {1}{0}", vbCrLf, e.Message)
Console.WriteLine("Stack Trace:{0} {1}{0}", vbCrLf, e.StackTrace)
End Try
End Sub
Private Sub ShowOccurrences(s As String, toFind As String, indexes As Integer())
Console.Write("'{0}' occurs at the following character positions: ",
toFind)
For ctr As Integer = 0 To indexes.Length - 1
Console.Write("{0}{1}", indexes(ctr),
If(ctr = indexes.Length - 1, "", ", "))
Next
Console.WriteLine()
End Sub
End Module
' The example displays the following output:
' 'a' occurs at the following character positions: 4, 7, 15
'
' An exception (ArgumentNullException) occurred.
' Message:
' Value cannot be null.
' Parameter name: value
'
' Stack Trace:
' at System.String.IndexOf(String value, Int32 startIndex, Int32 count, Stri
' ngComparison comparisonType)
' at Library.FindOccurrences(String s, String f)
' at Example.Main()
対照的に、次のステートメントを使用して例外を再スローする場合:
throw e;
Throw e
raise e
...その場合、完全な呼び出し履歴は保持されず、この例では次の出力が生成されます。
'a' occurs at the following character positions: 4, 7, 15
An exception (ArgumentNullException) occurred.
Message:
Value cannot be null.
Parameter name: value
Stack Trace:
at Library.FindOccurrences(String s, String f)
at Example.Main()
もう少し面倒な方法は、新しい例外をスローし、元の例外の呼び出し履歴情報を内部例外に保持することです。 呼び出し元は、新しい例外の InnerException プロパティを使用して、スタック フレームとその他の元の例外に関する情報を取得できます。 この場合、throw ステートメントは次のようになります。
throw new ArgumentNullException("You must supply a search string.", e);
raise (ArgumentNullException("You must supply a search string.", e) )
Throw New ArgumentNullException("You must supply a search string.",
e)
例外を処理するユーザー コードは、次の例外ハンドラーが示すように、 InnerException プロパティに元の例外に関する情報が含まれていることを認識する必要があります。
try
{
indexes = s.FindOccurrences(toFind);
ShowOccurrences(s, toFind, indexes);
}
catch (ArgumentNullException e)
{
Console.WriteLine($"An exception ({e.GetType().Name}) occurred.");
Console.WriteLine($" Message:{Environment.NewLine}{e.Message}");
Console.WriteLine($" Stack Trace:{Environment.NewLine} {e.StackTrace}");
Exception ie = e.InnerException;
if (ie != null)
{
Console.WriteLine(" The Inner Exception:");
Console.WriteLine($" Exception Name: {ie.GetType().Name}");
Console.WriteLine($" Message: {ie.Message}{Environment.NewLine}");
Console.WriteLine($" Stack Trace:{Environment.NewLine} {ie.StackTrace}{Environment.NewLine}");
}
}
// The example displays the following output:
// 'a' occurs at the following character positions: 4, 7, 15
//
// An exception (ArgumentNullException) occurred.
// Message: You must supply a search string.
//
// Stack Trace:
// at Library.FindOccurrences(String s, String f)
// at Example.Main()
//
// The Inner Exception:
// Exception Name: ArgumentNullException
// Message: Value cannot be null.
// Parameter name: value
//
// Stack Trace:
// at System.String.IndexOf(String value, Int32 startIndex, Int32 count, Stri
// ngComparison comparisonType)
// at Library.FindOccurrences(String s, String f)
try
let indexes = findOccurrences s toFind
showOccurrences toFind indexes
with :? ArgumentNullException as e ->
printfn $"An exception ({e.GetType().Name}) occurred."
printfn $" Message:\n{e.Message}"
printfn $" Stack Trace:\n {e.StackTrace}"
let ie = e.InnerException
if ie <> null then
printfn " The Inner Exception:"
printfn $" Exception Name: {ie.GetType().Name}"
printfn $" Message: {ie.Message}\n"
printfn $" Stack Trace:\n {ie.StackTrace}\n"
// The example displays the following output:
// 'a' occurs at the following character positions: 4, 7, 15
//
// An exception (ArgumentNullException) occurred.
// Message: You must supply a search string.
//
// Stack Trace:
// at Library.FindOccurrences(String s, String f)
// at Example.Main()
//
// The Inner Exception:
// Exception Name: ArgumentNullException
// Message: Value cannot be null.
// Parameter name: value
//
// Stack Trace:
// at System.String.IndexOf(String value, Int32 startIndex, Int32 count, Stri
// ngComparison comparisonType)
// at Library.FindOccurrences(String s, String f)
Try
indexes = s.FindOccurrences(toFind)
ShowOccurrences(s, toFind, indexes)
Catch e As ArgumentNullException
Console.WriteLine("An exception ({0}) occurred.",
e.GetType().Name)
Console.WriteLine(" Message: {1}{0}", vbCrLf, e.Message)
Console.WriteLine(" Stack Trace:{0} {1}{0}", vbCrLf, e.StackTrace)
Dim ie As Exception = e.InnerException
If ie IsNot Nothing Then
Console.WriteLine(" The Inner Exception:")
Console.WriteLine(" Exception Name: {0}", ie.GetType().Name)
Console.WriteLine(" Message: {1}{0}", vbCrLf, ie.Message)
Console.WriteLine(" Stack Trace:{0} {1}{0}", vbCrLf, ie.StackTrace)
End If
End Try
' The example displays the following output:
' 'a' occurs at the following character positions: 4, 7, 15
'
' An exception (ArgumentNullException) occurred.
' Message: You must supply a search string.
'
' Stack Trace:
' at Library.FindOccurrences(String s, String f)
' at Example.Main()
'
' The Inner Exception:
' Exception Name: ArgumentNullException
' Message: Value cannot be null.
' Parameter name: value
'
' Stack Trace:
' at System.String.IndexOf(String value, Int32 startIndex, Int32 count, Stri
' ngComparison comparisonType)
' at Library.FindOccurrences(String s, String f)
標準例外の選択
例外をスローする必要がある場合は、多くの場合、カスタム例外を実装する代わりに、.NET で既存の例外の種類を使用できます。 次の 2 つの条件で標準の例外の種類を使用する必要があります。
使用エラー、つまりメソッドを呼び出した開発者のプログラムロジックミスによる例外を投げています。 通常、ArgumentException、ArgumentNullException、InvalidOperationException、またはNotSupportedExceptionのような例外をスローします。 例外オブジェクトをインスタンス化するときに例外オブジェクトのコンストラクターに指定する文字列は、開発者が修正できるようにエラーを記述する必要があります。 詳細については、 Message プロパティを参照してください。
既存の .NET 例外を使用して呼び出し元に通知できるエラーを処理しています。 可能な限り具体的な例外をスローする必要があります。 たとえば、メソッドで引数が列挙型の有効なメンバーである必要がある場合は、InvalidEnumArgumentExceptionではなくArgumentException (最も派生クラス) をスローする必要があります。
次の表では、一般的な例外の種類と、それらを発生させる状況を示します。
例外 | 条件 |
---|---|
ArgumentException | メソッドに渡される null 以外の引数が無効です。 |
ArgumentNullException | メソッドに渡される引数は null 。 |
ArgumentOutOfRangeException | 引数が有効な値の範囲外です。 |
DirectoryNotFoundException | ディレクトリ パスの一部が無効です。 |
DivideByZeroException | 整数または Decimal 除算演算の分母は 0 です。 |
DriveNotFoundException | ドライブが使用できないか、存在しません。 |
FileNotFoundException | ファイルが存在しません。 |
FormatException | 値が、 Parse などの変換メソッドによって文字列から変換される適切な形式ではありません。 |
IndexOutOfRangeException | インデックスが配列またはコレクションの範囲外です。 |
InvalidOperationException | オブジェクトの現在の状態では、メソッド呼び出しが無効です。 |
KeyNotFoundException | コレクション内のメンバーにアクセスするための指定されたキーが見つかりません。 |
NotImplementedException | メソッドまたは操作は実装されていません。 |
NotSupportedException | メソッドまたは操作はサポートされていません。 |
ObjectDisposedException | 破棄されたオブジェクトに対して操作が実行されます。 |
OverflowException | 算術演算、キャスト演算、または変換操作を実行すると、オーバーフローが発生します。 |
PathTooLongException | パスまたはファイル名が、システム定義の最大長を超えています。 |
PlatformNotSupportedException | この操作は、現在のプラットフォームではサポートされていません。 |
RankException | 次元数が正しくない配列がメソッドに渡されます。 |
TimeoutException | 操作に割り当てられた時間間隔の有効期限が切れています。 |
UriFormatException | 無効な Uniform Resource Identifier (URI) が使用されています。 |
カスタム例外を実装する
次の場合、既存の .NET 例外を使用してエラー状態を処理することは十分ではありません。
例外が、既存の .NET 例外にマップできない一意のプログラム エラーを反映している場合。
例外で、既存の .NET 例外に適した処理とは異なる処理が必要な場合、または例外を同様の例外から明確に区別する必要がある場合。 たとえば、対象の整数型の範囲外にある文字列の数値表現を解析するときに ArgumentOutOfRangeException 例外をスローする場合、呼び出し元がメソッドを呼び出すときに適切な制約付き値を指定しなかった結果のエラーに対して同じ例外を使用しないようにします。
Exception クラスは、.NET のすべての例外の基底クラスです。 多くの派生クラスは、 Exception クラスのメンバーの継承された動作に依存します。 Exceptionのメンバーをオーバーライドしたり、一意のメンバーを定義したりすることはありません。
独自の例外クラスを定義するには:
Exceptionから継承するクラスを定義します。 必要に応じて、例外に関する追加情報を提供するためにクラスに必要な一意のメンバーを定義します。 たとえば、 ArgumentException クラスには、引数が例外の原因となったパラメーターの名前を指定する ParamName プロパティが含まれており、 RegexMatchTimeoutException プロパティにはタイムアウト間隔を示す MatchTimeout プロパティが含まれています。
必要であれば、機能を変更または修正したい継承されたメンバーをオーバーライドします。 Exceptionの既存の派生クラスのほとんどは、継承されたメンバーの動作をオーバーライドしないことに注意してください。
カスタム例外オブジェクトがシリアル化可能かどうかを判断します。 シリアル化を使用すると、例外に関する情報を保存でき、リモート処理コンテキストでサーバーとクライアント プロキシによって例外情報を共有できます。 例外オブジェクトをシリアル化できるようにするには、 SerializableAttribute 属性でマークします。
例外クラスのコンストラクターを定義します。 通常、例外クラスには次のコンストラクターが 1 つ以上含まれます。
Exception()は、既定値を使用して新しい例外オブジェクトのプロパティを初期化します。
Exception(String): 指定したエラー メッセージを使用して新しい例外オブジェクトを初期化します。
Exception(String, Exception): 指定されたエラー メッセージと内部例外を使用して新しい例外オブジェクトを初期化します。
Exception(SerializationInfo, StreamingContext)は、シリアル化されたデータから新しい例外オブジェクトを初期化する
protected
コンストラクターです。 例外オブジェクトをシリアル化可能にすることを選択した場合は、このコンストラクターを実装する必要があります。
次の例は、カスタム例外クラスの使用を示しています。 これは、クライアントが素数ではない開始番号を指定して素数のシーケンスを取得しようとしたときにスローされる NotPrimeException
例外を定義します。 例外は、例外の原因となった非素数を返す新しいプロパティ NonPrime
を定義します。
SerializationInfo クラスは、保護されたパラメーターなしのコンストラクターと、シリアル化のためのStreamingContextパラメーターとNotPrimeException
パラメーターを持つコンストラクターを実装するだけでなく、NonPrime
プロパティをサポートする 3 つの追加コンストラクターを定義します。 各コンストラクターは、素数以外の値を保持するだけでなく、基底クラスのコンストラクターを呼び出します。
NotPrimeException
クラスは、SerializableAttribute属性でもマークされます。
using System;
using System.Runtime.Serialization;
[Serializable()]
public class NotPrimeException : Exception
{
private int notAPrime;
protected NotPrimeException()
: base()
{ }
public NotPrimeException(int value) :
base(String.Format("{0} is not a prime number.", value))
{
notAPrime = value;
}
public NotPrimeException(int value, string message)
: base(message)
{
notAPrime = value;
}
public NotPrimeException(int value, string message, Exception innerException) :
base(message, innerException)
{
notAPrime = value;
}
protected NotPrimeException(SerializationInfo info,
StreamingContext context)
: base(info, context)
{ }
public int NonPrime
{ get { return notAPrime; } }
}
namespace global
open System
open System.Runtime.Serialization
[<Serializable>]
type NotPrimeException =
inherit Exception
val notAPrime: int
member this.NonPrime =
this.notAPrime
new (value) =
{ inherit Exception($"%i{value} is not a prime number."); notAPrime = value }
new (value, message) =
{ inherit Exception(message); notAPrime = value }
new (value, message, innerException: Exception) =
{ inherit Exception(message, innerException); notAPrime = value }
// F# does not support protected members
new () =
{ inherit Exception(); notAPrime = 0 }
new (info: SerializationInfo, context: StreamingContext) =
{ inherit Exception(info, context); notAPrime = 0 }
Imports System.Runtime.Serialization
<Serializable()> _
Public Class NotPrimeException : Inherits Exception
Private notAPrime As Integer
Protected Sub New()
MyBase.New()
End Sub
Public Sub New(value As Integer)
MyBase.New(String.Format("{0} is not a prime number.", value))
notAPrime = value
End Sub
Public Sub New(value As Integer, message As String)
MyBase.New(message)
notAPrime = value
End Sub
Public Sub New(value As Integer, message As String, innerException As Exception)
MyBase.New(message, innerException)
notAPrime = value
End Sub
Protected Sub New(info As SerializationInfo,
context As StreamingContext)
MyBase.New(info, context)
End Sub
Public ReadOnly Property NonPrime As Integer
Get
Return notAPrime
End Get
End Property
End Class
次の例に示す PrimeNumberGenerator
クラスは、Eratosthenes のシーブを使用して、クラス コンストラクターの呼び出しでクライアントによって指定された 2 から制限までの素数のシーケンスを計算します。
GetPrimesFrom
メソッドは、指定した下限以上のすべての素数を返しますが、その下限が素数でない場合はNotPrimeException
をスローします。
using System;
using System.Collections.Generic;
[Serializable]
public class PrimeNumberGenerator
{
private const int START = 2;
private int maxUpperBound = 10000000;
private int upperBound;
private bool[] primeTable;
private List<int> primes = new List<int>();
public PrimeNumberGenerator(int upperBound)
{
if (upperBound > maxUpperBound)
{
string message = String.Format(
"{0} exceeds the maximum upper bound of {1}.",
upperBound, maxUpperBound);
throw new ArgumentOutOfRangeException(message);
}
this.upperBound = upperBound;
// Create array and mark 0, 1 as not prime (True).
primeTable = new bool[upperBound + 1];
primeTable[0] = true;
primeTable[1] = true;
// Use Sieve of Eratosthenes to determine prime numbers.
for (int ctr = START; ctr <= (int)Math.Ceiling(Math.Sqrt(upperBound));
ctr++)
{
if (primeTable[ctr]) continue;
for (int multiplier = ctr; multiplier <= upperBound / ctr; multiplier++)
if (ctr * multiplier <= upperBound) primeTable[ctr * multiplier] = true;
}
// Populate array with prime number information.
int index = START;
while (index != -1)
{
index = Array.FindIndex(primeTable, index, (flag) => !flag);
if (index >= 1)
{
primes.Add(index);
index++;
}
}
}
public int[] GetAllPrimes()
{
return primes.ToArray();
}
public int[] GetPrimesFrom(int prime)
{
int start = primes.FindIndex((value) => value == prime);
if (start < 0)
throw new NotPrimeException(prime, String.Format("{0} is not a prime number.", prime));
else
return primes.FindAll((value) => value >= prime).ToArray();
}
}
namespace global
open System
[<Serializable>]
type PrimeNumberGenerator(upperBound) =
let start = 2
let maxUpperBound = 10000000
let primes = ResizeArray()
let primeTable =
upperBound + 1
|> Array.zeroCreate<bool>
do
if upperBound > maxUpperBound then
let message = $"{upperBound} exceeds the maximum upper bound of {maxUpperBound}."
raise (ArgumentOutOfRangeException message)
// Create array and mark 0, 1 as not prime (True).
primeTable[0] <- true
primeTable[1] <- true
// Use Sieve of Eratosthenes to determine prime numbers.
for i = start to float upperBound |> sqrt |> ceil |> int do
if not primeTable[i] then
for multiplier = i to upperBound / i do
if i * multiplier <= upperBound then
primeTable[i * multiplier] <- true
// Populate array with prime number information.
let mutable index = start
while index <> -1 do
index <- Array.FindIndex(primeTable, index, fun flag -> not flag)
if index >= 1 then
primes.Add index
index <- index + 1
member _.GetAllPrimes() =
primes.ToArray()
member _.GetPrimesFrom(prime) =
let start =
Seq.findIndex ((=) prime) primes
if start < 0 then
raise (NotPrimeException(prime, $"{prime} is not a prime number.") )
else
Seq.filter ((>=) prime) primes
|> Seq.toArray
Imports System.Collections.Generic
<Serializable()> Public Class PrimeNumberGenerator
Private Const START As Integer = 2
Private maxUpperBound As Integer = 10000000
Private upperBound As Integer
Private primeTable() As Boolean
Private primes As New List(Of Integer)
Public Sub New(upperBound As Integer)
If upperBound > maxUpperBound Then
Dim message As String = String.Format(
"{0} exceeds the maximum upper bound of {1}.",
upperBound, maxUpperBound)
Throw New ArgumentOutOfRangeException(message)
End If
Me.upperBound = upperBound
' Create array and mark 0, 1 as not prime (True).
ReDim primeTable(upperBound)
primeTable(0) = True
primeTable(1) = True
' Use Sieve of Eratosthenes to determine prime numbers.
For ctr As Integer = START To CInt(Math.Ceiling(Math.Sqrt(upperBound)))
If primeTable(ctr) Then Continue For
For multiplier As Integer = ctr To CInt(upperBound \ ctr)
If ctr * multiplier <= upperBound Then primeTable(ctr * multiplier) = True
Next
Next
' Populate array with prime number information.
Dim index As Integer = START
Do While index <> -1
index = Array.FindIndex(primeTable, index, Function(flag)
Return Not flag
End Function)
If index >= 1 Then
primes.Add(index)
index += 1
End If
Loop
End Sub
Public Function GetAllPrimes() As Integer()
Return primes.ToArray()
End Function
Public Function GetPrimesFrom(prime As Integer) As Integer()
Dim start As Integer = primes.FindIndex(Function(value)
Return value = prime
End Function)
If start < 0 Then
Throw New NotPrimeException(prime, String.Format("{0} is not a prime number.", prime))
Else
Return primes.FindAll(Function(value)
Return value >= prime
End Function).ToArray()
End If
End Function
End Class
次の例では、非素数を使用して GetPrimesFrom
メソッドを 2 回呼び出します。そのうちの 1 つはアプリケーション ドメインの境界を越えます。 どちらの場合も、例外がスローされ、クライアント コードで正常に処理されます。
using System;
using System.Reflection;
class Example1
{
public static void Main()
{
int limit = 10000000;
PrimeNumberGenerator primes = new PrimeNumberGenerator(limit);
int start = 1000001;
try
{
int[] values = primes.GetPrimesFrom(start);
Console.WriteLine($"There are {start} prime numbers from {limit} to {2}");
}
catch (NotPrimeException e)
{
Console.WriteLine($"{e.NonPrime} is not prime");
Console.WriteLine(e);
Console.WriteLine("--------");
}
AppDomain domain = AppDomain.CreateDomain("Domain2");
PrimeNumberGenerator gen = (PrimeNumberGenerator)domain.CreateInstanceAndUnwrap(
typeof(Example).Assembly.FullName,
"PrimeNumberGenerator", true,
BindingFlags.Default, null,
new object[] { 1000000 }, null, null);
try
{
start = 100;
Console.WriteLine(gen.GetPrimesFrom(start));
}
catch (NotPrimeException e)
{
Console.WriteLine($"{e.NonPrime} is not prime");
Console.WriteLine(e);
Console.WriteLine("--------");
}
}
}
open System
open System.Reflection
let limit = 10000000
let primes = PrimeNumberGenerator limit
let start = 1000001
try
let values = primes.GetPrimesFrom start
printfn $"There are {values.Length} prime numbers from {start} to {limit}"
with :? NotPrimeException as e ->
printfn $"{e.NonPrime} is not prime"
printfn $"{e}"
printfn "--------"
let domain = AppDomain.CreateDomain "Domain2"
let gen =
domain.CreateInstanceAndUnwrap(
typeof<PrimeNumberGenerator>.Assembly.FullName,
"PrimeNumberGenerator", true,
BindingFlags.Default, null,
[| box 1000000 |], null, null)
:?> PrimeNumberGenerator
try
let start = 100
printfn $"{gen.GetPrimesFrom start}"
with :? NotPrimeException as e ->
printfn $"{e.NonPrime} is not prime"
printfn $"{e}"
printfn "--------"
Imports System.Reflection
Module Example
Sub Main()
Dim limit As Integer = 10000000
Dim primes As New PrimeNumberGenerator(limit)
Dim start As Integer = 1000001
Try
Dim values() As Integer = primes.GetPrimesFrom(start)
Console.WriteLine("There are {0} prime numbers from {1} to {2}",
start, limit)
Catch e As NotPrimeException
Console.WriteLine("{0} is not prime", e.NonPrime)
Console.WriteLine(e)
Console.WriteLine("--------")
End Try
Dim domain As AppDomain = AppDomain.CreateDomain("Domain2")
Dim gen As PrimeNumberGenerator = domain.CreateInstanceAndUnwrap(
GetType(Example).Assembly.FullName,
"PrimeNumberGenerator", True,
BindingFlags.Default, Nothing,
{1000000}, Nothing, Nothing)
Try
start = 100
Console.WriteLine(gen.GetPrimesFrom(start))
Catch e As NotPrimeException
Console.WriteLine("{0} is not prime", e.NonPrime)
Console.WriteLine(e)
Console.WriteLine("--------")
End Try
End Sub
End Module
' The example displays the following output:
' 1000001 is not prime
' NotPrimeException: 1000001 is not a prime number.
' at PrimeNumberGenerator.GetPrimesFrom(Int32 prime)
' at Example.Main()
' --------
' 100 is not prime
' NotPrimeException: 100 is not a prime number.
' at PrimeNumberGenerator.GetPrimesFrom(Int32 prime)
' at Example.Main()
' --------
例示
次の例は、catch
エラーを処理するために定義されたwith
(F# でArithmeticException) ブロックを示しています。
catch
はDivideByZeroExceptionから派生し、DivideByZeroExceptionエラーに対して明示的に定義ArithmeticExceptionブロックがないため、このcatch
ブロックはDivideByZeroExceptionエラーもキャッチします。
using System;
class ExceptionTestClass
{
public static void Main()
{
int x = 0;
try
{
int y = 100 / x;
}
catch (ArithmeticException e)
{
Console.WriteLine($"ArithmeticException Handler: {e}");
}
catch (Exception e)
{
Console.WriteLine($"Generic Exception Handler: {e}");
}
}
}
/*
This code example produces the following results:
ArithmeticException Handler: System.DivideByZeroException: Attempted to divide by zero.
at ExceptionTestClass.Main()
*/
module ExceptionTestModule
open System
let x = 0
try
let y = 100 / x
()
with
| :? ArithmeticException as e ->
printfn $"ArithmeticException Handler: {e}"
| e ->
printfn $"Generic Exception Handler: {e}"
// This code example produces the following results:
// ArithmeticException Handler: System.DivideByZeroException: Attempted to divide by zero.
// at <StartupCode$fs>.$ExceptionTestModule.main@()
Class ExceptionTestClass
Public Shared Sub Main()
Dim x As Integer = 0
Try
Dim y As Integer = 100 / x
Catch e As ArithmeticException
Console.WriteLine("ArithmeticException Handler: {0}", e.ToString())
Catch e As Exception
Console.WriteLine("Generic Exception Handler: {0}", e.ToString())
End Try
End Sub
End Class
'
'This code example produces the following results:
'
'ArithmeticException Handler: System.OverflowException: Arithmetic operation resulted in an overflow.
' at ExceptionTestClass.Main()
'
.NET