www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - What's the equivalent of std::runtime_error in D?

reply alexandr3000 <sp0148675 gmail.com> writes:
```d
if (vkCreateInstance(&createInfo, null, &instance) != VK_SUCCESS) 
{
             ???
}
```
Nov 09 2022
next sibling parent reply MorteFeuille123 <MorteFeuille123 jo.rt> writes:
On Wednesday, 9 November 2022 at 12:25:35 UTC, alexandr3000 wrote:
 ```d
 if (vkCreateInstance(&createInfo, null, &instance) != 
 VK_SUCCESS) {
             ???
 }
 ```
The official error handling system is based on exception, e.g ```d 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
Nov 09 2022
next sibling parent alexandr3000 <sp0148675 gmail.com> writes:
On Wednesday, 9 November 2022 at 13:47:09 UTC, MorteFeuille123 
wrote:
 On Wednesday, 9 November 2022 at 12:25:35 UTC, alexandr3000 
 wrote:
 ```d
 if (vkCreateInstance(&createInfo, null, &instance) != 
 VK_SUCCESS) {
             ???
 }
 ```
The official error handling system is based on exception, e.g ```d 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
Nov 09 2022
prev sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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. :)
Nov 09 2022
prev sibling parent Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Wednesday, 9 November 2022 at 12:25:35 UTC, alexandr3000 wrote:
 ```d
 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
Nov 09 2022