loading...

Raise Exceptions in D

github logo ・1 min read

Python (14 Part Series)

1) Boolean in D 2) Comments in D 3 ... 12 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 11) Filter a Dictionary in D 12) Split, Apply, Merge in D 13) Map Reduce in D 14) Skip the first line of a file 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 1 '19

What the heck is a Developer Advocate?

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

Tip: You can configure your dev.to reading experience, such as using sans serif fonts.

Go to your the "misc." section of your settings.

Customize dev.to