Создать свой собственный класс исключений в D очень просто – достаточно произвести наследование от класса Exception.
Вот самый элементарный пример:
// исключение, обозначающее, что файл программы не найден
class ProgramNotFoundException : Exception
{
public
{
@safe pure nothrow this(
string message,
string file = __FILE__,
size_t line = __LINE__,
Throwable next = null)
{
super(message, file, line, next);
}
}
}
Вызвать такое исключение еще проще:
throw new ProgramNotFoundException("Important part of this program not found");