This example shows how you can throw and catch the ad-hoc exception types.
// you can throw and catch by matching the string and argument types try { // throw it with parenthesis after the template args (it uses opCall to construct) throw ArsdException!"Test"(); // you could also `throw new ArsdException!"test";`, but that gets harder with args // as we'll see in the following example assert(0); // remove from docs } catch(ArsdException!"Test" e) { // catch it without them // this has no useful information except for the type // but you can catch it like this and it is still more than generic Exception } // an exception's job is to deliver useful information up the chain // and you can do that easily by passing arguments: try { throw ArsdException!"Test"(4, "four"); // you could also `throw new ArsdException!("Test", int, string)(4, "four")` // but now you start to see how the opCall convenience constructor simplifies things assert(0); // remove from docs } catch(ArsdException!("Test", int, string) e) { // catch it and use info by specifying types assert(e.data[0] == 4); // and extract arguments like this assert(e.data[1] == "four"); } // a throw site can add additional information without breaking code that catches just some // generally speaking, each additional argument creates a new subclass on top of the previous args // so you can cast try { throw ArsdException!"Test"(4, "four", 9); assert(0); // remove from docs } catch(ArsdException!("Test", int, string) e) { // this catch still works assert(e.data[0] == 4); assert(e.data[1] == "four"); // but if you were to print it, all the members would be there // import std.stdio; writeln(e); // would show something like: /+ ArsdException!("Test", int, string, int)@file.d(line): [0] int: 4 [1] string: four [2] int: 9 +/ // indicating that there's additional information available if you wanted to process it // and meanwhile: ArsdException!("Test", int) e2 = e; // this implicit cast works thanks to the parent-child relationship ArsdException!"Test" e3 = e; // this works too, the base type/string still matches // so catching those types would work too }
This is a generic exception with attached arguments. It is used when I had to throw something but didn't want to write a new class.
You can catch an ArsdException to get its passed arguments out.
You can pass either a base class or a string as Type.
See the examples for how to use it.