英語版
このページの英語版を見る

std.exception

このモジュールは、例外と一般的なエラー処理に関する関数を定義する。 を定義する。また、単体テストを支援するための関数も定義している。

ソース std/exception.d

Examples:
シノピス
import core.stdc.stdlib : malloc, free;
import std.algorithm.comparison : equal;
import std.algorithm.iteration : map, splitter;
import std.algorithm.searching : endsWith;
import std.conv : ConvException, to;
import std.range : front, retro;

// assertのようにenforceを使う
int a = 3;
enforce(a > 2, "a needs to be higher than 2.");

// enforceはカスタム例外をスローすることができる
enforce!ConvException(a > 2, "a needs to be higher than 2.");

// enforceは入力を返す
enum size = 42;
auto memory = enforce(malloc(size), "malloc failed")[0 .. size];
scope(exit) free(memory.ptr);

// collectExceptionは例外のテストに使われる
Exception e = collectException("abc".to!int);
assert(e.file.endsWith("conv.d"));

// そして例外メッセージのためだけに
string msg = collectExceptionMsg("abc".to!int);
writeln(msg); // "Unexpected 'a' when converting from type string to type int"(文字列型からint型への変換時に予期せぬ'a'が発生した)

// assertThrownは、例外がスローされたことを表明するために使うことができる
assertThrown!ConvException("abc".to!int);

// ifThrownを使うと、例外がスローされたときのデフォルト値を指定できる
writeln("x".to!int().ifThrown(0)); // 0

// handleは、範囲用のifThrownのより高度なバージョンである
auto r = "12,1337z32,54".splitter(',').map!(a => to!int(a));
auto h = r.handle!(ConvException, RangePrimitive.front, (e, r) => 0);
assert(h.equal([12, 0, 54]));
assertThrown!ConvException(h.retro.equal([54, 0, 12]));

// basicExceptionCtorsは、カスタム例外を作成する際に定型的な記述を回避する
static class MeaCulpa : Exception
{
    mixin basicExceptionCtors;
}
e = collectException((){throw new MeaCulpa("diagnostic message");}());
writeln(e.msg); // "diagnostic message"(診断メッセージ)
writeln(e.file); // __FILE__
writeln(e.line); // __LINE__ - 3

// assumeWontThrowは、スローするコードを`nothrow`にキャストするために使用できる
void exceptionFreeCode() nothrow
{
    // 自動デコードは、無効なUTF文字が与えられた場合にのみスローする
    assumeWontThrow("abc".front);
}

// assumeUniqueは、変更可能なインスタンスを`immutable`にキャストするために使用できる
// 使用には注意が必要だ
char[] str = "  mutable".dup;
str[0 .. 2] = "im";
immutable res = assumeUnique(str);
writeln(res); // "immutable"
auto assertNotThrown(T : Throwable = Exception, E)(lazy E expression, string msg = null, string file = __FILE__, size_t line = __LINE__);
指定された式が、指定された "型"をスローしないことを保証する。 Throwable 与えられた型のThrowable がスローされた場合、それは捕捉され、assertNotThrown をエスケープしない、 をエスケープしない。むしろ AssertError がスローされる。しかし、それ以外のThrowableはエスケープされる。
Parameters:
T テストするThrowable
E expression テストする式。
string msg テスト失敗時に出力するメッセージ(オプション)。 msg が空で、スローされた例外に が空でない場合は、例外の msg フィールドがテスト失敗時に出力される。 フィールドが出力される。
string file エラーが発生したファイル。 デフォルトは__FILE__
size_t line エラーが発生した行。 デフォルトは__LINE__
Throws:
AssertError 与えられた がスローされた場合。 Throwable
Returns:
の結果である。 expression.
Examples:
import core.exception : AssertError;

import std.string;
assertNotThrown!StringException(enforce!StringException(true, "Error!"));

//例外はデフォルトである。
assertNotThrown(enforce!StringException(true, "Error!"));

assert(collectExceptionMsg!AssertError(assertNotThrown!StringException(
           enforce!StringException(false, "Error!"))) ==
       `assertNotThrown failed: StringException was thrown: Error!`);
void assertThrown(T : Throwable = Exception, E)(lazy E expression, string msg = null, string file = __FILE__, size_t line = __LINE__);
与えられた式が与えられた型Throwable をスローすることを表明する。 Throwable は捕捉され、assertThrown をエスケープしない。しかし ThrowableはエスケープされるThrowable がスローされない場合、AssertError がスローされる。
Parameters:
T テストするThrowable
E expression テストする式。
string msg テスト失敗時に出力するメッセージ(オプション)。
string file エラーが発生したファイル。 デフォルトは__FILE__
size_t line エラーが発生した行。 デフォルトは__LINE__
Throws:
AssertError 与えられた がスローされなかった場合。Throwable
Examples:
import core.exception : AssertError;
import std.string;

assertThrown!StringException(enforce!StringException(false, "Error!"));

//例外はデフォルトである。
assertThrown(enforce!StringException(false, "Error!"));

assert(collectExceptionMsg!AssertError(assertThrown!StringException(
           enforce!StringException(true, "Error!"))) ==
       `assertThrown failed: No StringException was thrown.`);
template enforce(E : Throwable = Exception) if (is(typeof(new E("", string.init, size_t.init)) : Throwable) || is(typeof(new E(string.init, size_t.init)) : Throwable))

T enforce(T, Dg, string file = __FILE__, size_t line = __LINE__)(T value, scope Dg dg)
if (isSomeFunction!Dg && is(typeof(dg())) && is(typeof(() { if (!value) { } } )));

T enforce(T)(T value, lazy Throwable ex);
与えられた値が真であることを強制する。 与えられた値が偽の場合、例外がスローされる。 その
  • msg - エラーメッセージはstring
  • dg - 文字列を返すカスタムデリゲートで、例外が発生した場合にのみ呼び出される。
  • ex - スローされるカスタム例外。これはlazy で、例外が発生した場合にのみ作成される。
Parameters:
T value テストする値。
E 値がfalseと評価された場合にスローする例外型".
const(char)[] msg 例外がスローされた場合のエラーメッセージ。
Dg dg 値がfalseと評価された場合に呼び出されるデリゲート。
Throwable ex 値がfalseと評価された場合にスローする例外。
string file 呼び出し元のソース・ファイル。
size_t line 呼び出し元の行番号。
Returns:
valueもし cast(bool) valueが真の場合。そうでなければ 選択されたオーバーロードに応じて、new Exception(msg)dg()または exがスローされる。
注釈:」である: enforceは例外をスローするために使用される。 エラー処理を助けるためのものである。プログラムのロジックを検証するためのものではない。 それはassert
それは。 enforceの中(すなわち、inout ブロックやinvariantの内部など)、契約は-releaseでコンパイルする際にコンパイルアウトされるからだ。 releaseでコンパイルするときに、コントラクトはコンパイルアウトされるからだ。
デリゲートが渡された場合、この関数の安全性と純度は の安全性と純度から推測される。 Dg の安全性と純度から推測される。
Examples:
import core.stdc.stdlib : malloc, free;
import std.conv : ConvException, to;

// assertのようにenforceを使う
int a = 3;
enforce(a > 2, "a needs to be higher than 2.");

// enforceはカスタム例外をスローすることができる
enforce!ConvException(a > 2, "a needs to be higher than 2.");

// enforceは入力を返す
enum size = 42;
auto memory = enforce(malloc(size), "malloc failed")[0 .. size];
scope(exit) free(memory.ptr);
Examples:
assertNotThrown(enforce(true, new Exception("this should not be thrown")));
assertThrown(enforce(false, new Exception("this should be thrown")));
Examples:
writeln(enforce(123)); // 123

try
{
    enforce(false, "error");
    assert(false);
}
catch (Exception e)
{
    writeln(e.msg); // "error"
    writeln(e.file); // __FILE__
    writeln(e.line); // __LINE__ - 7
}
Examples:
独自の"enforce関数"にエイリアスを付ける。
import std.conv : ConvException;
alias convEnforce = enforce!ConvException;
assertNotThrown(convEnforce(true));
assertThrown!ConvException(convEnforce(false, "blah"));
T enforce(T)(T value, lazy const(char)[] msg = null, string file = __FILE__, size_t line = __LINE__)
if (is(typeof(() { if (!value) { } } )));
alias errnoEnforce = enforce!(ErrnoException).enforce(T)(T value, lazy const(char)[] msg = null, string file = __FILE__, size_t line = __LINE__) if (is(typeof(() { if (!value) { } } )));
与えられた値が真であることを強制し、真でない場合はErrnoException を投げる。 を投げる。
Parameters:
T value テストする値。
const(char)[] msg スローされた場合にErrnoException に含めるメッセージ。
Returns:
valueもしcast(bool) value がtrueの場合。そうでなければ new ErrnoException(msg) がスローされる。 最後の errno を失敗した条件に対応するエラーコードに設定したものと仮定される。 に対応するエラーコードを設定したと仮定される。
Examples:
import core.stdc.stdio : fclose, fgets, fopen;
import std.file : thisExePath;
import std.string : toStringz;

auto f = fopen(thisExePath.toStringz, "r").errnoEnforce;
scope(exit) fclose(f);
char[100] buf;
auto line = fgets(buf.ptr, buf.length, f);
enforce(line !is null); // 空でない行を期待する
T collectException(T = Exception, E)(lazy E expression, ref E result);
与えられた式からスローされた例外をキャッチして返す。 例外がスローされなかった場合はnullが返され resultが返される。 式の結果に設定される。
ただし collectException限らず、あらゆる Throwable を収集するために使用できるが、Exceptionに限らず、一般的に から派生した "型"でもExceptionException.だから collectExceptionを使ってはいけない。 Exceptionの派生型でもないものをキャッチするのは、一般的に好ましくない。 しないこと。
Parameters:
T キャッチする例外の型。
E expression 例外をスローする可能性のある式。
E result 例外がスローされなかった場合の式の結果。
Examples:
int b;
int foo() { throw new Exception("blah"); }
assert(collectException(foo(), b));

version (D_NoBoundsChecks) {}
else
{
    // 境界外エラーをチェックする
    int[] a = new int[3];
    import core.exception : RangeError;
    assert(collectException!RangeError(a[4], b));
}
T collectException(T : Throwable = Exception, E)(lazy E expression);
与えられた式からスローされた例外をキャッチして返す。 例外がスローされなかった場合はNULLが返される。Evoid.
である。 collectException限らず、あらゆる Throwable を収集するために使うことができるが、Exceptionに限らず、一般的には から派生した "型"でもExceptionException.だから collectExceptionを使ってはいけない。 Exceptionの派生型でもないものをキャッチするのは、一般的に好ましくない。 しないこと。
Parameters:
T キャッチする例外の型。
E expression 例外をスローする可能性のある式。
Examples:
int foo() { throw new Exception("blah"); }
writeln(collectException(foo()).msg); // "blah"
string collectExceptionMsg(T = Exception, E)(lazy E expression);
与えられた式からスローされる例外をキャッチし、その例外の msg プロパティを返す。 msg プロパティを返す。例外がスローされない場合は、NULLが返される。 が返される。E にはvoid を指定できる。
例外がスローされたが、メッセージが空である場合は emptyExceptionMsg が返される。
注釈: collectExceptionMsg限らず、あらゆる Throwable を収集するために使用できるが、Exceptionに限らず、一般的に から派生した "型"でもExceptionException.だから collectExceptionMsgを使ってはいけない。 Exceptionの派生型でもないものをキャッチするのは、一般的に好ましくない。 しないこと。
Parameters:
T キャッチする例外の型。
E expression 例外をスローする可能性のある式。
Examples:
void throwFunc() { throw new Exception("My Message."); }
writeln(collectExceptionMsg(throwFunc())); // "My Message."

void nothrowFunc() {}
assert(collectExceptionMsg(nothrowFunc()) is null);

void throwEmptyFunc() { throw new Exception(""); }
writeln(collectExceptionMsg(throwEmptyFunc())); // 空の例外メッセージ
enum string emptyExceptionMsg;
collectExceptionMsgが空の例外メッセージを持つ例外をキャッチしたときに返す値。 が返す値。
pure nothrow immutable(T)[] assumeUnique(T)(T[] array);

pure nothrow immutable(T)[] assumeUnique(T)(ref T[] array);

pure nothrow immutable(T[U]) assumeUnique(T, U)(ref T[U] array);
イディオムな方法で、変更可能配列を不変配列にキャストする。 に変換する。技術的には assumeUniqueはキャストを挿入するだけである、 しかし、その名前は呼び出し側の仮定を文書化したものである。 を呼び出す。 assumeUnique(arr)が呼び出されるのは arr 。この仮定を強化する、 assumeUnique(arr) も、戻る前にarr をクリアする。基本的に、assumeUnique(arr) は、 の要素にもうこれ以上変更可能なアクセスがないことを、呼び出し元がコミットしていることを示す。 arrは変更可能 "である。 (推移的に)変更可能なアクセスはもうない。 によって返される不変配列を通して行われる。 assumeUnique.
一般的に assumeUniqueによって返される不変配列を経由して行われる。 関数から配列を返すときに使われる。
Parameters:
T[] array immutableにキャストする配列。
Returns:
不変配列。

例:

string letters()
{
  char[] result = new char['z' - 'a' + 1];
  foreach (i, ref e; result)
  {
    e = cast(char)('a' + i);
  }
  return assumeUnique(result);
}
上記の例での使い方は正しい。resultletters のプライベートであり、関数が戻った後、参照したメモリはもはや に書き込むことができないからである。以下の例では の間違った使い方を示している。 assumeUnique.

悪いことだ。

char[] buffer;
string letters(char first, char last)
{
  if (first >= last) return null; // fine
  auto sneaky = buffer;
  sneaky.length = last - first + 1;
  foreach (i, ref e; sneaky)
  {
    e = cast(char)('a' + i);
  }
  return assumeUnique(sneaky); // BAD
}
上記の例は、前の呼び出し元が不変とみなしていた返された配列を変更するため、クライアントコードを大混乱に陥れる。 を変更するからである。書き込み可能な配列から 書き込み可能な配列buffer から不変配列を得るには、最後の行を次のように置き換える。 最後の行を
return to!(string)(sneaky); // もうそんなにずるくはない
to 、配列は適切に複製される。
注釈:コンパイル時に一意であるかどうかをチェックすることは、特に関数が一意である場合に可能である。 特に関数が としてマーク(または推論)されている場合である。 pureとしてマークされている(あるいは推測されている)場合は特にそうである。以下の例では、以下の関数を呼び出す必要はない。 を呼び出す必要はない。 assumeUniqueを呼び出す必要はない。 なぜなら、コンパイラーは純粋関数内の配列の一意性を推論できるからである:
static string letters() pure
{
  char[] result = new char['z' - 'a' + 1];
  foreach (i, ref e; result)
  {
    e = cast(char)('a' + i);
  }
  return result;
}
一意性の推測については、unique を参照のこと。 lent キーワードを参照のこと。 や ArchJava のキーワードを参照してほしい。
を使うことの欠点は assumeUnique's を使うことの欠点は、現時点では ということである; 一方、慣用的な assumeUniqueの慣用的な使い方は の慣用的な用法は単純で、許容できるほどまれなものである。

Examples:
int[] arr = new int[1];
auto arr1 = arr.assumeUnique;
static assert(is(typeof(arr1) == immutable(int)[]));
writeln(arr); // null
writeln(arr1); // [0]
Examples:
int[string] arr = ["a":1];
auto arr1 = arr.assumeUnique;
static assert(is(typeof(arr1) == immutable(int[string])));
writeln(arr); // null
writeln(arr1.keys); // ["a"]
nothrow T assumeWontThrow(T)(lazy T expr, string msg = null, string file = __FILE__, size_t line = __LINE__);
nothrow 、 関数から呼び出されるようにする。 をnothrow 関数から呼び出せるようにする。
このラッパー関数は、呼び出し側のコミットメントを文書化する。 このラッパー関数は、呼び出し側のコミットメントを文書化する。 の評価中に例外が発生する可能性がある。 expr. もし 式が実行時にスローされることが判明した場合、このラッパーは AssertError.
(をスローする(Throwable のサブクラスでないAssertError のような オブジェクトは、 関数からもスローされる可能性があることに注意)。 Exception のようなオブジェクトは、nothrow 関数からもスローされる可能性がある、 (サブクラス化されていない のようなオブジェクトは、 関数からでもスローされる可能性があることに注意されたい。 )。
Parameters:
T expr スローしないことを表明する式。
string msg 仮定が偽であることが判明した場合にAssertError に含めるメッセージ。 に含めるメッセージ。
string file 呼び出し元のソースファイル名。
size_t line 呼び出し元の行番号。
Returns:
の値。 exprの値(もしあれば)。
Examples:
import std.math.algebraic : sqrt;

// この関数はスローする可能性がある。
int squareRoot(int x)
{
    if (x < 0)
        throw new Exception("Tried to take root of negative number");
    return cast(int) sqrt(cast(double) x);
}

// この関数は決してスローしない。
int computeLength(int x, int y) nothrow
{
    // x*x + y*yは常に正であるため、squareRootがスローしないと
    // 安全に仮定でき、このnothrow関数を実装するためにそれを使用できる。
    // スローする場合(例えば、x*x + y*yが32ビット値でオーバーフローする場合)、
    // プログラムは終了する。
    return assumeWontThrow(squareRoot(x*x + y*y));
}

writeln(computeLength(3, 4)); // 5
pure nothrow @nogc @trusted bool doesPointTo(S, T, Tdummy = void)(auto const ref S source, const ref T target)
if (__traits(isRef, source) || isDynamicArray!S || is(S == U*, U) || is(S == class));

pure nothrow @trusted bool doesPointTo(S, T)(auto const shared ref S source, const shared ref T target);

pure nothrow @trusted bool mayPointTo(S, T, Tdummy = void)(auto const ref S source, const ref T target)
if (__traits(isRef, source) || isDynamicArray!S || is(S == U*, U) || is(S == class));

pure nothrow @trusted bool mayPointTo(S, T)(auto const shared ref S source, const shared ref T target);
与えられたソース・オブジェクトが、与えられたターゲット・オブジェクトへのポインタまたは参照を含んでいるかどうかをチェックする。 へのポインタまたは参照があるかどうかを調べる。
Parameters:
S source ソース・オブジェクト
T target ターゲット・オブジェクト
Bugs:
推論が失敗する可能性があるため、関数は明示的に@nogcBugzilla issue 17084を参照のこと。
Returns:
true もし sourceを指すポインターを埋め込んでいる。 を指す targetを指すポインターを埋め込む。 を指すポインタを埋め込む。
もし sourceが動的配列であるか、または動的配列を含んでいる場合、これらの関数は、動的配列と動的配列の間に重なりがあるかどうかをチェックする。 との間に重なりがあるかどうかをチェックする。 targetの表現が重複しているかどうかをチェックする。
もし sourceがクラスであれば、ポインタとして扱われる。
もし targetがポインタ、動的配列、またはクラスである場合、これらの関数は以下のチェックのみを行う。 ポインタが sourceを指しているかどうかだけをチェックする。 targetを指すかどうかだけをチェックするtargetを指すかどうかだけをチェックする。
もし sourceが"共用体"またはvoid[n] を含む場合、偽陽性が発生する可能性がある。 偽陰性になる可能性がある:
doesPointToは、絶対確実ならtrue を返す。 sourceを指す。 target.偽陰性を返すことはあっても、偽陽性を返すことはない。 を返す。この関数は、入力データを検証しようとするときに優先されるべきである。 を優先すべきである。
mayPointTo絶対確実な場合はfalse を返す。 sourceを指していない。 target.偽陽性を返すことはあっても、偽陰性を返すことはない。 を返さない。この関数は、防御的にコード・パスを選択するのに使われる。 この関数を優先すべきである。

注釈: doesPointTo(x, x) x 。 内部ポインタがあるかどうかをチェックする。これは断定的なテストとしてのみ行うべきである、 言語がオブジェクトに内部ポインタがないと仮定するのは自由だからだ。 (tdpl 7.1.3.5)。

Examples:
ポインタ
int  i = 0;
int* p = null;
assert(!p.doesPointTo(i));
p = &i;
assert( p.doesPointTo(i));
Examples:
構造と共用体
struct S
{
    int v;
    int* p;
}
int i;
auto s = S(0, &i);

// 構造体と共用体は、それらのメンバーを"所有"する
// pointsToは、メンバの1つがpointsToであればtrueを返す。
assert(!s.doesPointTo(s.v)); // s.vはsのvメンバであり、pointedではない。
assert( s.p.doesPointTo(i)); //iはs.pによってポイントされる。
assert( s  .doesPointTo(i)); //つまり、iはs自身によって指し示されていることになる。

// 共用体もまったく同じように動作する。ポイントは、同じメモリーを
// 共有している場合でも、各"メンバー"を個別に確認する
Examples:
配列(動的および静的)
int i;
 // スライスを初期化する際にコンパイラを欺く
 // https://issues.dlang.org/show_bug.cgi?id=18637
int* p = &i;
int[]  slice = [0, 1, 2, 3, 4];
int[5] arr   = [0, 1, 2, 3, 4];
int*[]  slicep = [p];
int*[1] arrp   = [&i];

// スライスはそのメンバーすべてを指す:
assert( slice.doesPointTo(slice[3]));
assert(!slice[0 .. 2].doesPointTo(slice[3])); // オブジェクト3は
                                              // スライス [0 ... 2]の外側にある

// スライスは、そのメンバーが指し示すものを考慮しないことに注意。
assert( slicep[0].doesPointTo(i));
assert(!slicep   .doesPointTo(i));

// 静的配列は、構造体のように、そのメンバーを所有するオブジェクトである:
assert(!arr.doesPointTo(arr[0])); // arr[0]はarrの単なるメンバなので、
                                  // ポインタではない。
assert( arrp[0].doesPointTo(i));  // iはarrp[0]が指す。
assert( arrp   .doesPointTo(i));  // つまり、iはarrp自体によって
                                  // 指し示されている。

// 静的配列と動的配列の違いに注目しよう:
assert(!arr  .doesPointTo(arr[0]));
assert( arr[].doesPointTo(arr[0]));
assert( arrp  .doesPointTo(i));
assert(!arrp[].doesPointTo(i));
Examples:
クラス
class C
{
    this(int* p){this.p = p;}
    int* p;
}
int i;
C a = new C(&i);
C b = a;

// クラスは、クラスペイロードへの単純なポインタのように扱われるため、
// 少し特殊である。
assert( a.p.doesPointTo(i)); // a.pはiを指す。
assert(!a  .doesPointTo(i)); // しかしa自体はiを指していない。

//クラスペイロード自体を確認するには、そのメンバーを反復処理する:
()
{
    import std.traits : Fields;

    foreach (index, _; Fields!C)
        if (doesPointTo(a.tupleof[index], i))
            return;
    assert(0);
}();

// クラスが特定のペイロードを指しているかどうかを確認するには、
// 直接的なメモリーチェックを行うことができる:
auto aLoc = cast(ubyte[__traits(classInstanceSize, C)]*) a;
assert(b.doesPointTo(*aLoc)); // bはaが指している場所を指す
class ErrnoException: object.Exception;
errno を設定するエラーが発生した場合にスローされる。
Examples:
import core.stdc.errno : EAGAIN;
auto ex = new ErrnoException("oh no", EAGAIN);
writeln(ex.errno); // EAGAIN
Examples:
明示的なエラーコードが提供されていない場合、デフォルトで errno が使用される。
import core.stdc.errno : errno, EAGAIN;

auto old = errno;
scope(exit) errno = old;

// errnoが着呼側によって設定されたことを偽る
errno = EAGAIN;
auto ex = new ErrnoException("oh no");
writeln(ex.errno); // EAGAIN
final pure nothrow @nogc @property scope @safe uint errno();
オペレーティングシステムのエラーコード。
final pure nothrow @nogc @property scope @safe string errnoMsg();
ローカライズされたエラーメッセージは core.stdc.string.strerror_rまたは core.stdc.string.strerror.
@safe this(string msg, string file = null, size_t line = 0);
エラーメッセージを受け取るコンストラクタ。現在のグローバルな core.stdc.errno.errno値がエラーコードとして使用される。
@safe this(string msg, int errno, string file = null, size_t line = 0);
エラーメッセージとエラーコードを受け取るコンストラクタ。
CommonType!(T1, T2) ifThrown(E : Throwable = Exception, T1, T2)(lazy scope T1 expression, lazy scope T2 errorHandler);

CommonType!(T1, T2) ifThrown(E : Throwable, T1, T2)(lazy scope T1 expression, scope T2 delegate(E) errorHandler);

CommonType!(T1, T2) ifThrown(T1, T2)(lazy scope T1 expression, scope T2 delegate(Exception) errorHandler);
MLスタイルの関数型例外処理。与えられた式を実行し その結果を返す。式がThrowable をスローした場合、代わりに与えられたエラーハンドラを実行し、その結果を返す。 がスローされた場合、代わりに与えられたエラーハンドラを実行し、その結果を返す。エラーハンドラの の型は、式の型と同じでなければならない。
Parameters:
E キャッチするThrowablesの型。デフォルトはException
T1 式の型。
T2 エラー・ハンドラの戻り値の型。
T1 expression 実行し、結果を返す式。
T2 errorHandler 式がスローされた場合に実行するハンドラ。
Returns:
式がスローされなければ、その結果を返す。それ以外の場合は の結果を返す。
Examples:
エラー時にデフォルト値に戻す:
import std.conv : to;
writeln("x".to!int.ifThrown(0)); // 0
Examples:
ifThrownを複数回連鎖させ、それぞれが直前の式全体のエラーを捕捉する。 エラーを捕捉する。
import std.conv : ConvException, to;
string s = "true";
assert(s.to!int.ifThrown(cast(int) s.to!double)
               .ifThrown(cast(int) s.to!bool) == 1);

s = "2.0";
assert(s.to!int.ifThrown(cast(int) s.to!double)
               .ifThrown(cast(int) s.to!bool) == 2);

// エラーの種類によって対応が異なる
alias orFallback = (lazy a)  => a.ifThrown!ConvException("not a number")
                                 .ifThrown!Exception("number too small");

writeln(orFallback(enforce("x".to!int < 1).to!string)); // "not a number"(数字ではない)
writeln(orFallback(enforce("2".to!int < 1).to!string)); // "number too small"(小さすぎる数字)
Examples:
式とerrorHandlerは、暗黙的にキャストできる共通の型を持たなければならない。 に暗黙的にキャストできる共通の型を持たなければならない。 式の型となる。
// nullとnew Objectは共通の型(Object)を持つ。
static assert(is(typeof(null.ifThrown(new Object())) == Object));
static assert(is(typeof((new Object()).ifThrown(null)) == Object));

// 1とnew Objectは共通の型を持たない。
static assert(!__traits(compiles, 1.ifThrown(new Object())));
static assert(!__traits(compiles, (new Object()).ifThrown(1)));
Examples:
ラムダを使ってスローされたオブジェクトを取得する。
import std.format : format;
// "std.format.FormatException"
writeln("%s".format.ifThrown!Exception(e => e.classinfo.name));
enum RangePrimitive: int;
このenumhandleプリミティブを選択するために使われる。enum の値は、OR'dすることができる。 で複数のプリミティブを選択することができる。
RangePrimitive.accessはアクセスプリミティブのショートカットである。frontback およびopIndex
RangePrimitive.popは変異プリミティブのショートカットである; popFront そしてpopBack
Examples:
import std.algorithm.comparison : equal;
import std.algorithm.iteration : map, splitter;
import std.conv : to, ConvException;

auto s = "12,1337z32,54,2,7,9,1z,6,8";

// 次の行の構成は、入力の一部の要素が整数に変換されないため、
// 反復処理時にスローされる
auto r = s.splitter(',').map!(a => to!int(a));

// ConvExceptionの場合には0を代入する
auto h = r.handle!(ConvException, RangePrimitive.front, (e, r) => 0);
assert(h.equal([12, 0, 54, 2, 7, 9, 0, 6, 8]));
Examples:
import std.algorithm.comparison : equal;
import std.range : retro;
import std.utf : UTFException;

auto str = "hello\xFFworld"; // 0xFFは無効なUTF-8コードユニットである

auto handled = str.handle!(UTFException, RangePrimitive.access,
        (e, r) => ' '); // 無効なコードポイントをスペースに置き換える

assert(handled.equal("hello world")); // `front`が処理され、
assert(handled.retro.equal("dlrow olleh")); // `back`も同様
front

back

popFront

popBack

empty

save

length

opDollar

opIndex

opSlice

access

pop
auto handle(E : Throwable, RangePrimitive primitivesToHandle, alias handler, Range)(Range input)
if (isInputRange!Range);
レンジプリミティブからスローされる例外を処理する。
どのプリミティブを処理するかを指定するには RangePrimitive列挙型を使用して、処理するプリミティブを指定する。 複数の範囲プリミティブを一度に処理するには、OR 演算子を使用する。 または擬似プリミティブRangePrimitive.accessRangePrimitive.pop を使うことで一度に複数のプリミティブを扱うことができる。 処理されるすべてのプリミティブは、ユーザーが提供するハンドラと互換性のある戻り値型または値を持たなければならない。 プリミティブと互換性のある戻り値または値を持たなければならない。
Parameters:
E 扱うThrowable の型。
primitivesToHandle 扱う範囲プリミティブのセット。
handler をスローしたときに呼び出される callable。 Throwable E をスローしたときに呼び出される callable。ハンドラは ハンドラはE, ref IRange 形式の引数を受け入れなければならない。 E の戻り値として使用される。opIndex の場合、ハンドラはオプションで第3引数を受け取ることができる。 オプションで第3引数を受け取ることができる。
Range input 処理する範囲。
Returns:
の範囲インターフェイスを保持するラッパーstructinput.

注釈: スライスをサポートする無限範囲はインスタンスを返さなければならない。 スライシングをサポートする無限範囲は std.range.Takeのインスタンスを返さなければならない。 のインスタンスを返さなければならない ( std.range.primitives.hasSlicing); handleに対処する。 ハンドラ関数の戻り値からtake、0を返す。 例外がキャッチされたときにそれを返す。

Examples:
import std.algorithm.comparison : equal;
import std.algorithm.iteration : map, splitter;
import std.conv : to, ConvException;

auto s = "12,1337z32,54,2,7,9,1z,6,8";

// 次の行の構成は、入力の一部の要素が整数に変換されないため、
// 反復処理時にスローされる
auto r = s.splitter(',').map!(a => to!int(a));

// ConvExceptionの場合には0を代入する
auto h = r.handle!(ConvException, RangePrimitive.front, (e, r) => 0);
assert(h.equal([12, 0, 54, 2, 7, 9, 0, 6, 8]));
Examples:
import std.algorithm.comparison : equal;
import std.range : retro;
import std.utf : UTFException;

auto str = "hello\xFFworld"; // 0xFFは無効なUTF-8コードユニットである

auto handled = str.handle!(UTFException, RangePrimitive.access,
        (e, r) => ' '); // 無効なコードポイントをスペースに置き換える

assert(handled.equal("hello world")); // `front`が処理され、
assert(handled.retro.equal("dlrow olleh")); // `back`も同様
template basicExceptionCtors()
例外を簡単にサブクラス化するための便利なミックスイン
例外の些細なサブクラス化でさえ、次のような定型的なコードを書かなければならない。 を書く必要がある:1) 例外がスローされたソースファイルと行番号を正しく渡す。 で使えるようにする。 enforceで使用可能であること。 は、例外コンストラクタが一定の順序で引数を取ることを期待している。この ミックスインはその定型コードを提供する。
ただし、mixin の行に、最低限(つまり )DDocコメントをつける必要がある。 /// DDocコメントでマークする必要がある。 コンストラクタが新しく作成されたExceptionサブクラスでドキュメント化されるようにしたい場合は、少なくとも最小限の( )DDocコメントを付ける必要がある。
現在の制限:現在の制限 バグ#11500、 現在、このミックスインで指定されたコンストラクタを他のカスタムコンストラクタでオーバーロードすることはできない。 をオーバーロードすることはできない。そのため、このミックスインは現在のところ、カスタムコンストラクタを明示的に指定する必要がない場合にのみ使うことができる。 を明示的に指定する必要がない場合にのみ使用できる。
Examples:
class MeaCulpa: Exception
{
    ///
    mixin basicExceptionCtors;
}

try
    throw new MeaCulpa("test");
catch (MeaCulpa e)
{
    writeln(e.msg); // "test"
    writeln(e.file); // __FILE__
    writeln(e.line); // __LINE__ - 5
}
pure nothrow @nogc @safe this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null);
Parameters:
string msg 例外のメッセージ。
string file 例外が発生したファイル。
size_t line 例外が発生した行番号。
Throwable next 例外の連鎖の前の例外があれば、その例外。
pure nothrow @nogc @safe this(string msg, Throwable next, string file = __FILE__, size_t line = __LINE__);
Parameters:
string msg 例外のメッセージ。
Throwable next 例外の連鎖における前の例外。
string file 例外が発生したファイル。
size_t line 例外が発生した行番号。