digitalmars.D - What's the equivalent of std::runtime_error in D?
- alexandr3000 (6/6) Nov 09 2022 ```d
- MorteFeuille123 (19/25) Nov 09 2022 The official error handling system is based on exception, e.g
- alexandr3000 (3/29) Nov 09 2022 thanks
- =?UTF-8?Q?Ali_=c3=87ehreli?= (23/36) Nov 09 2022 I enjoy exceptions a little differently:
- Imperatorn (6/12) Nov 09 2022 See Ali's answer.
```d
if (vkCreateInstance(&createInfo, null, &instance) != VK_SUCCESS)
{
???
}
```
Nov 09 2022
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
On Wednesday, 9 November 2022 at 13:47:09 UTC, MorteFeuille123 wrote:On Wednesday, 9 November 2022 at 12:25:35 UTC, alexandr3000 wrote:thanks```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
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
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









alexandr3000 <sp0148675 gmail.com> 