raise

Function used to signal a failure and its proximate cause from integer math code. Depending on the value of the policy parameter, raise() will either:

template raise(IntFlagPolicy policy)
void
raise
pure @safe

Examples

1 import checkedint.throws : raise; // set IntFlagPolicy.throws
2 
3 bool caught = false;
4 try
5 {
6     raise(IntFlag.div0);
7 }
8 catch (CheckedIntException e)
9 {
10     caught = (e.intFlags == IntFlag.div0);
11 }
12 assert(caught);
1 import checkedint.asserts : raise; // set IntFlagPolicy.asserts
2 
3 bool caught = false;
4 try
5 {
6     raise(IntFlag.div0);
7 }
8 catch (Error e)
9 {
10     caught = (e.msg == "divide by zero");
11 }
12 assert(caught);
1 import checkedint.sticky : raise; // set IntFlagPolicy.sticky
2 
3 raise(IntFlag.div0);
4 raise(IntFlag.posOver);
5 
6 assert(IntFlags.local.clear() == (IntFlag.div0 | IntFlag.posOver));
1 // Multiple signaling strategies may be usefully mixed within the same program:
2 alias IFP = IntFlagPolicy;
3 
4 static void fails() @safe nothrow
5 {
6     raise!(IFP.sticky)(IntFlag.negOver);
7     raise!(IFP.sticky)(IntFlag.imag);
8 }
9 
10 bool caught = false;
11 try
12 {
13     fails();
14     // Flags that were raised by `nothrow` code can easily be turned into an exception by the caller.
15     raise!(IFP.throws)(IntFlags.local.clear());
16 }
17 catch (CheckedIntException e)
18 {
19     caught = (e.intFlags == (IntFlag.negOver | IntFlag.imag));
20 }
21 assert(caught);

Meta