Last Updated: February 25, 2016
·
901
· Paxa

Handle exceptions in D

Here's how to handle exceptions in D.

try {
    throw new Exception("error message");
} catch (Exception error) {
    writefln("Error catched: %s", error.msg);
} finally {
    writefln("in finaly block");
}

catch (Exception error) will catch all exception of type Exception and child (inhereted) types.

Error types structure

Here is a list of standart error types in D (not complete)

  • Throwable
    • Error
      • AssertError
      • FinalizeError
      • HiddenFuncError
      • InvalidMemoryOperationError
      • OutOfMemoryError
      • RangeError
      • SwitchError
    • Exception
      • ErrnoException
      • UnicodeException
      • FileException (std.file)
      • ProcessException (std.process)
      • RegexException (std.regex)
      • DateTimeException (std.datetime)
      • TimeException (core.time)
      • StdioException (std.stdio)
      • StringException (std.string)
      • AddressException, HostException, SocketException, ... (std.socket)

Custom errors

// Define custom exception class
class ArgumentError : Exception {
    this(string msg, string file = __FILE__, size_t line = __LINE__) {
        super(msg, file, line);
    }
}

// Throw custom class error
throw new ArgumentError("first argument array should be not empty");

// Catch custom error
try {
    someFunction([]);
} catch (ArgumentError error) {
    writeln("ERROR: ", error.msg);
    return 1;
}

We need to add string file = __FILE__, size_t line = __LINE__ which will automatically add position where exception was thrown, without it stacktrace will show line number of super(msg).

More

nothrow functions can not throw any exceptions. It's made to indicate for developers who will use your function, also for better compiled code generation.

ulong size() nothrow { return m_size; }

Read more: