Is there a way to replace Exception with as a macro in C?

Mike Parker aldacron at gmail.com
Tue Feb 19 06:16:59 UTC 2019


On Tuesday, 19 February 2019 at 05:50:04 UTC, yisooan wrote:
>
> This is allowed.
> But I want to do the exact same thing in D. I have already 
> tried some expressions with alias? but it doesn't work.
>

alias can't be used for expressions.

>
> Would you help me, please?

There's nothing exactly equivalent to the C preprocessor. The 
closest you'd be able to get without using a keyword like mixin 
is to use a function:

```
Exception invalidIndexException() { throw new Exception("Index is 
invalid"); }

if(false) invalidIndexException;
```

Of course, you can also subclass Exception:

```
class InvalidIndexException : Exception
{
     this(string file = __FILE__, size_t line = __LINE__)
     {
         super("Index is invalid", file, line);
     }
}

if(false) throw new InvalidIndexException;
```




More information about the Digitalmars-d-learn mailing list