Thread overview
What's the equivalent of std::runtime_error in D?
Nov 09, 2022
alexandr3000
Nov 09, 2022
MorteFeuille123
Nov 09, 2022
alexandr3000
Nov 09, 2022
Ali Çehreli
Nov 09, 2022
Imperatorn
November 09, 2022
if (vkCreateInstance(&createInfo, null, &instance) != VK_SUCCESS) {
            ???
}
November 09, 2022

On Wednesday, 9 November 2022 at 12:25:35 UTC, alexandr3000 wrote:

>
if (vkCreateInstance(&createInfo, null, &instance) != VK_SUCCESS) {
            ???
}

The official error handling system is based on exception, e.g

class VKException : Exception
{
    this(string msg)
    {
        super(msg);
    }
}

void _()
{
   if (vkCreateInstance(&createInfo, null, &instance) != VK_SUCCESS) {
      throw new VKException("cannot create VK instance")
   }
}

manual : https://dlang.org/spec/errors.html

November 09, 2022

On Wednesday, 9 November 2022 at 13:47:09 UTC, MorteFeuille123 wrote:

>

On Wednesday, 9 November 2022 at 12:25:35 UTC, alexandr3000 wrote:

>
if (vkCreateInstance(&createInfo, null, &instance) != VK_SUCCESS) {
            ???
}

The official error handling system is based on exception, e.g

class VKException : Exception
{
    this(string msg)
    {
        super(msg);
    }
}

void _()
{
   if (vkCreateInstance(&createInfo, null, &instance) != VK_SUCCESS) {
      throw new VKException("cannot create VK instance")
   }
}

manual : https://dlang.org/spec/errors.html

thanks

November 09, 2022
On 11/9/22 05:47, MorteFeuille123 wrote:

> class VKException : Exception
> {
>      this(string msg)
>      {
>          super(msg);
>      }
> }
>
> void _()
> {
>     if (vkCreateInstance(&createInfo, null, &instance) != VK_SUCCESS) {
>        throw new VKException("cannot create VK instance")
>     }
> }

I enjoy exceptions a little differently:

1) I rarely define my own exception types because most of the time I (and the user) care only about a meaningful error message.

(Having said that, during development, I sometimes print not 'exc.message' but 'exc' itself to view a stack trace even from an Exception. (I always print 'exc' for Error objects because a stack trace is almost the only thing for such programmer errors.))

2) I never see 'throw' in my code because I think std.exception.enforce makes the code more readable (note that != is now ==):

  enforce(vkCreateInstance(&createInfo, null, &instance) == VK_SUCCESS,
          "cannot create VK instance");

'enforce' can throw special exception types as well:

  enforce!VKException(/* ... */);

And for all vk* functions that should return VK_SUCCESS, the whole thing can be wrapped in a function template but one needs to learn from 'enforce's implementation to ensure file and line numbers are reported as well:

  vkEnforce(vkCreateInstance(&createInfo, null, &instance),
            "cannot create VK instance");

Ali

P.S. There is also the Learn forum, where such topics may be more effective. :)

November 09, 2022

On Wednesday, 9 November 2022 at 12:25:35 UTC, alexandr3000 wrote:

>
if (vkCreateInstance(&createInfo, null, &instance) != VK_SUCCESS) {
            ???
}

See Ali's answer.

Here's the link to enforce:
https://dlang.org/library/std/exception/enforce.html

And here's exception:
https://dlang.org/library/std/exception.html