digitalmars.D.learn - newbie problem with nothrow
- WhatMeWorry (22/22) Oct 31 2016 Is there a way to turn off nothrow or work around it? Because to
- Temtaime (2/25) Oct 31 2016 Wrap a body of the function to try {} catch {} and it'll work.
- Kapps (5/37) Oct 31 2016 Assuming you're sure it'll never throw. To enforce this, use try
- Jonathan M Davis via Digitalmars-d-learn (7/11) Oct 31 2016 I always use assert(0). e.g.
- Daniel9 (3/16) Nov 01 2016 have the same problem
- Steven Schveighoffer (4/14) Nov 01 2016 This turns into a non-printing seg fault when compiled in release mode.
- Jonathan M Davis via Digitalmars-d-learn (11/29) Nov 01 2016 wrote:
- Steven Schveighoffer (13/27) Nov 01 2016 I disagree that avoiding the message printing is not a problem. I have
- Jonathan M Davis via Digitalmars-d-learn (7/15) Nov 01 2016 assumeWontThrow uses lazy to do what it does (and I'm not sure that ther...
- Steven Schveighoffer (14/29) Nov 01 2016 I was thinking something like this:
- Mike Parker (12/17) Nov 01 2016 Not advisable in this case. This is for a function intended to be
- Mike Parker (42/65) Oct 31 2016 If you can't change the interface of CiricularQueue to be nothrow
Is there a way to turn off nothrow or work around it? Because to me it looks like nothrow prevents me from doing anything useful. extern(C) void onKeyEvent(GLFWwindow* window, int key, int scancode, int action, int modifier) nothrow { if(queue.roomInQueue()) { auto event = new Event; event.type = EventType.keyboard; event.keyboard.key = cast(Key) key; // etc. } Error: function 'event_handler.CircularQueue.roomInQueue' is not nothrow Error: function 'event_handler.onKeyEvent' is nothrow yet may throw The compiler wouldn't let me just remove "nothrow" from the function. I tried a kludge where I had this function just pass all its parameters to another throwable function, but this caused errors as well. So I'm stuck. Anyone know how to proceed. Thanks.
Oct 31 2016
On Monday, 31 October 2016 at 16:55:51 UTC, WhatMeWorry wrote:Is there a way to turn off nothrow or work around it? Because to me it looks like nothrow prevents me from doing anything useful. extern(C) void onKeyEvent(GLFWwindow* window, int key, int scancode, int action, int modifier) nothrow { if(queue.roomInQueue()) { auto event = new Event; event.type = EventType.keyboard; event.keyboard.key = cast(Key) key; // etc. } Error: function 'event_handler.CircularQueue.roomInQueue' is not nothrow Error: function 'event_handler.onKeyEvent' is nothrow yet may throw The compiler wouldn't let me just remove "nothrow" from the function. I tried a kludge where I had this function just pass all its parameters to another throwable function, but this caused errors as well. So I'm stuck. Anyone know how to proceed. Thanks.Wrap a body of the function to try {} catch {} and it'll work.
Oct 31 2016
On Monday, 31 October 2016 at 17:04:28 UTC, Temtaime wrote:On Monday, 31 October 2016 at 16:55:51 UTC, WhatMeWorry wrote:Assuming you're sure it'll never throw. To enforce this, use try { } catch { throw new Error("blah"); }. You can still throw errors, just not exceptions (as errors are not meant to be caught).Is there a way to turn off nothrow or work around it? Because to me it looks like nothrow prevents me from doing anything useful. extern(C) void onKeyEvent(GLFWwindow* window, int key, int scancode, int action, int modifier) nothrow { if(queue.roomInQueue()) { auto event = new Event; event.type = EventType.keyboard; event.keyboard.key = cast(Key) key; // etc. } Error: function 'event_handler.CircularQueue.roomInQueue' is not nothrow Error: function 'event_handler.onKeyEvent' is nothrow yet may throw The compiler wouldn't let me just remove "nothrow" from the function. I tried a kludge where I had this function just pass all its parameters to another throwable function, but this caused errors as well. So I'm stuck. Anyone know how to proceed. Thanks.Wrap a body of the function to try {} catch {} and it'll work.
Oct 31 2016
On Monday, October 31, 2016 22:20:59 Kapps via Digitalmars-d-learn wrote:Assuming you're sure it'll never throw. To enforce this, use try { } catch { throw new Error("blah"); }. You can still throw errors, just not exceptions (as errors are not meant to be caught).I always use assert(0). e.g. try return format("%s", 42); catch(Exception) assert(0, "format threw when it shouldn't be possible."); - Jonathan M Davis
Oct 31 2016
On Monday, 31 October 2016 at 22:29:19 UTC, Jonathan M Davis wrote:On Monday, October 31, 2016 22:20:59 Kapps via Digitalmars-d-learn wrote:have the same problemAssuming you're sure it'll never throw. To enforce this, use try { } catch { throw new Error("blah"); }. You can still throw errors, just not exceptions (as errors are not meant to be caught).I always use assert(0). e.g. try return format("%s", 42); catch(Exception) assert(0, "format threw when it shouldn't be possible."); - Jonathan M Davis
Nov 01 2016
On 10/31/16 6:29 PM, Jonathan M Davis via Digitalmars-d-learn wrote:On Monday, October 31, 2016 22:20:59 Kapps via Digitalmars-d-learn wrote:This turns into a non-printing seg fault when compiled in release mode. Is there not some assumeNoThrow wrapper somewhere? -SteveAssuming you're sure it'll never throw. To enforce this, use try { } catch { throw new Error("blah"); }. You can still throw errors, just not exceptions (as errors are not meant to be caught).I always use assert(0). e.g. try return format("%s", 42); catch(Exception) assert(0, "format threw when it shouldn't be possible.");
Nov 01 2016
On Tuesday, November 01, 2016 10:57:38 Steven Schveighoffer via Digitalmars- d-learn wrote:On 10/31/16 6:29 PM, Jonathan M Davis via Digitalmars-d-learn wrote:wrote:On Monday, October 31, 2016 22:20:59 Kapps via Digitalmars-d-learnI'm well aware of that, and I don't see that as a problem. A message might be nice, but the key thing is that it kills the program if there's a problem, and given Mike's point about the C layer, having it segfault is potentially preferable to throwing an Error to kill the program.This turns into a non-printing seg fault when compiled in release mode.Assuming you're sure it'll never throw. To enforce this, use try { } catch { throw new Error("blah"); }. You can still throw errors, just not exceptions (as errors are not meant to be caught).I always use assert(0). e.g. try return format("%s", 42); catch(Exception) assert(0, "format threw when it shouldn't be possible.");Is there not some assumeNoThrow wrapper somewhere?Someone added assemWontThrow to std.exception semi-recently, but I'd be very surprised if it didn't incur performance overhead such that I'd just as soon use an explicit try-catch and be done with it. - Jonathan M Davis
Nov 01 2016
On 11/1/16 11:54 AM, Jonathan M Davis via Digitalmars-d-learn wrote:On Tuesday, November 01, 2016 10:57:38 Steven Schveighoffer via Digitalmars- d-learn wrote:I disagree that avoiding the message printing is not a problem. I have had the unpleasant experience of having a program that dies on the order of 1-2 weeks with "SegFault", without any way of instrumenting the failure (debugging not an option, doesn't fail on dev machine). Just a tiny hint of what the problem is, can save weeks if not months of searching. A little while ago, I added an internal tool to both abort a program, and print a message (including file and line number) to druntime. It's not exactly public, but may be useful to expose. It should be callable from any location, including signal handlers. https://github.com/dlang/druntime/blob/master/src/core/internal/abort.dOn 10/31/16 6:29 PM, Jonathan M Davis via Digitalmars-d-learn wrote:I'm well aware of that, and I don't see that as a problem. A message might be nice, but the key thing is that it kills the program if there's a problem, and given Mike's point about the C layer, having it segfault is potentially preferable to throwing an Error to kill the program.assert(0, "format threw when it shouldn't be possible.");This turns into a non-printing seg fault when compiled in release mode.The function I'm imagining shouldn't add any overhead... -SteveIs there not some assumeNoThrow wrapper somewhere?Someone added assemWontThrow to std.exception semi-recently, but I'd be very surprised if it didn't incur performance overhead such that I'd just as soon use an explicit try-catch and be done with it.
Nov 01 2016
On Tuesday, November 01, 2016 12:19:11 Steven Schveighoffer via Digitalmars- d-learn wrote:On 11/1/16 11:54 AM, Jonathan M Davis via Digitalmars-d-learn wrote:assumeWontThrow uses lazy to do what it does (and I'm not sure that there's any other way to do it other than string mixins), and as I understand it, lazy isn't exactly efficient. Given that it always calls the resulting delegate though, the optimizer may be able to remove the extra cost. - Jonathan M DavisOn Tuesday, November 01, 2016 10:57:38 Steven Schveighoffer viaThe function I'm imagining shouldn't add any overhead...Is there not some assumeNoThrow wrapper somewhere?Someone added assemWontThrow to std.exception semi-recently, but I'd be very surprised if it didn't incur performance overhead such that I'd just as soon use an explicit try-catch and be done with it.
Nov 01 2016
On 11/1/16 12:24 PM, Jonathan M Davis via Digitalmars-d-learn wrote:On Tuesday, November 01, 2016 12:19:11 Steven Schveighoffer via Digitalmars- d-learn wrote:I was thinking something like this: auto assumeNoThrow(alias foo, Args...)(Args args) nothrow { try { return foo(args); } catch(Exception e) { throw new Error("foo should not have done that!"); } } -SteveOn 11/1/16 11:54 AM, Jonathan M Davis via Digitalmars-d-learn wrote:assumeWontThrow uses lazy to do what it does (and I'm not sure that there's any other way to do it other than string mixins), and as I understand it, lazy isn't exactly efficient. Given that it always calls the resulting delegate though, the optimizer may be able to remove the extra cost.On Tuesday, November 01, 2016 10:57:38 Steven Schveighoffer viaThe function I'm imagining shouldn't add any overhead...Is there not some assumeNoThrow wrapper somewhere?Someone added assemWontThrow to std.exception semi-recently, but I'd be very surprised if it didn't incur performance overhead such that I'd just as soon use an explicit try-catch and be done with it.
Nov 01 2016
On Monday, 31 October 2016 at 22:20:59 UTC, Kapps wrote:Not advisable in this case. This is for a function intended to be used as a C callback. There's no guarantee any errors thrown will propagate through the C side back into the D side. I wrote an article about this on gamedev.net [1] a while back. The feedback there is what led me to finally declare all of the callback types in Derelict (like the GLFW key callback above) as nothrow. I've since refined my approach, but, unless things have changes significantly (and I don't believe they have) the general idea still holds. [1] http://www.gamedev.net/page/resources/_/technical/general-programming/d-exceptions-and-c-callbacks-r3323Wrap a body of the function to try {} catch {} and it'll work.Assuming you're sure it'll never throw. To enforce this, use try { } catch { throw new Error("blah"); }. You can still throw errors, just not exceptions (as errors are not meant to be caught).
Nov 01 2016
On Monday, 31 October 2016 at 16:55:51 UTC, WhatMeWorry wrote:Is there a way to turn off nothrow or work around it? Because to me it looks like nothrow prevents me from doing anything useful. extern(C) void onKeyEvent(GLFWwindow* window, int key, int scancode, int action, int modifier) nothrow { if(queue.roomInQueue()) { auto event = new Event; event.type = EventType.keyboard; event.keyboard.key = cast(Key) key; // etc. } Error: function 'event_handler.CircularQueue.roomInQueue' is not nothrow Error: function 'event_handler.onKeyEvent' is nothrow yet may throw The compiler wouldn't let me just remove "nothrow" from the function. I tried a kludge where I had this function just pass all its parameters to another throwable function, but this caused errors as well. So I'm stuck. Anyone know how to proceed. Thanks.If you can't change the interface of CiricularQueue to be nothrow and don't want to wrap everything in a try...catch block, then just use a dynamic array. You can also use the old C union trick to avoid the need to allocate every event. That's the same thing SDL does. Something like this: ``` enum EventType { key, mouse, } struct KeyEvent { EventType type; ... this(...) { this.type = EventType.key; ... } } struct MouseEvent { EventType type; ... this(...) { this.type = EventType.mouse; ... } } union Event { EventType type; KeyEvent key; MouseEvent mouse; } Event[] eventq; extern(C) void onKeyEvent(GLFWwindow* window, int key, int scancode, int action, int modifier) nothrow { Event event; event.key = KeyEvent(window, key, scancode, action, modifier); eventq ~= event; } ```
Oct 31 2016