Raise Exceptions in D

github logo ・1 min read

Python (10 Part Series)

1) Boolean in D 2) Comments in D 3 ... 8 3) Raise Exceptions in D 4) Add to a List in D 5) Count of a List in D 6) String Contained in a String in D 7) Where did my string go in D 8) Case Insensitive in D 9) Add to a Dictionary in D 10) Check Dictionary has key in D

I had a desire to throw exceptions in my Python script. Well here is what you need to know in D.

throw new Exception("Error message.");

import std.exception;
enforce(true, "Error message");

The standard library provides a function to check a condition, if false it throws with the error message.

It is also possible to specify a custom exception to utilize. In D anything deriving from Throwable can be thrown or caught. Error derives from Throwable and is not intended to be caught as the compiler may not unwind the stack, thus not calling except handling code (no guarantee the catch will be called).

class MyException : Exception {
... /// https://tour.dlang.org/tour/en/basics/exceptions
twitter logo DISCUSS
Classic DEV Post from Jun 20

What does a #codeNewbie need to start/improve?

Jesse Phillips profile image
Senior Quality Assurance (SDET) starting to establish a larger presence in communities.

dev.to now has dark mode.

Go to the "misc" section of your settings and select night theme ❤️