Jump to page: 1 2
Thread overview
Trailing catch on function?
Nov 07, 2016
Manu
Nov 07, 2016
Jacob Carlborg
Nov 07, 2016
Walter Bright
Nov 08, 2016
Walter Bright
Nov 08, 2016
Mike Parker
Nov 08, 2016
Manu
Nov 08, 2016
Anonymouse
Nov 08, 2016
Daniel N
Nov 08, 2016
Manu
Nov 09, 2016
Daniel N
Nov 12, 2016
Anonymouse
Nov 13, 2016
André Puel
November 07, 2016
Hey people, I'm passing lots of D function pointers to C, and naturally, the C api expects the fp signatures are all nothrow.

Which means, my D functions all look like this:

void callback() nothrow
{
try
{
...lots of code...
}
catch (Exception e)
{
...log error or abort...
}
}


I'm generally annoyed by all the extra indentation.
Since nothrow is a central thing in D, I wondered if it would be reasonable
to allow a nice little bit of sugar to assist working with nothrow that
would look like this:

void catchingCallback() nothrow
{
...lots of code...
}
catch (Exception e)
{
...log error or abort...
}

Syntactically similar to the existing in/out sections, although I think it
would be nicer at the bottom...
It's simply a lowering which wraps the entire function body scope in a try.

It's not a really big deal, but it would feel a whole lot tidier in my
existing code.
I don't imagine how it would really affect anything else.

Terrible idea?


November 07, 2016
On 2016-11-07 05:30, Manu via Digitalmars-d wrote:
> Hey people, I'm passing lots of D function pointers to C, and naturally,
> the C api expects the fp signatures are all nothrow.
>
> Which means, my D functions all look like this:
>
> void callback() nothrow
> {
> try
> {
> ...lots of code...
> }
> catch (Exception e)
> {
> ...log error or abort...
> }
> }
>
>
> I'm generally annoyed by all the extra indentation.
> Since nothrow is a central thing in D, I wondered if it would be
> reasonable to allow a nice little bit of sugar to assist working with
> nothrow that would look like this:
>
> void catchingCallback() nothrow
> {
> ...lots of code...
> }
> catch (Exception e)
> {
> ...log error or abort...
> }
>
> Syntactically similar to the existing in/out sections, although I think
> it would be nicer at the bottom...
> It's simply a lowering which wraps the entire function body scope in a try.
>
> It's not a really big deal, but it would feel a whole lot tidier in my
> existing code.
> I don't imagine how it would really affect anything else.
>
> Terrible idea?

No, I like it. Ruby supports this feature. But I think I would prefer the catch inside the body. That works nicely when not curly braces are required.

BTW, this has been proposed before, but I cannot find it in the newsgroup.

-- 
/Jacob Carlborg
November 07, 2016
On 11/6/2016 8:30 PM, Manu via Digitalmars-d wrote:
> Hey people, I'm passing lots of D function pointers to C, and naturally, the C
> api expects the fp signatures are all nothrow.
>
> Which means, my D functions all look like this:
>
> void callback() nothrow
> {
> try
> {
> ...lots of code...
> }
> catch (Exception e)
> {
> ...log error or abort...
> }
> }
>
>
> I'm generally annoyed by all the extra indentation.

void callback() nothrow
{
    scope (failure)
    {
        ...log error or abort...
    }
    ...lots of code...
}

November 08, 2016
On Monday, 7 November 2016 at 23:37:18 UTC, Walter Bright wrote:
> void callback() nothrow
> {
>     scope (failure)
>     {
>         ...log error or abort...
>     }
>     ...lots of code...
> }

Who to get the Exception thrown in the scope(failure)
November 08, 2016
On Tuesday, 8 November 2016 at 00:12:09 UTC, Robert burner Schadek wrote:

> Who to get the Exception thrown in the scope(failure)

Who to --> How do you ...
November 07, 2016
On 11/7/2016 4:12 PM, Robert burner Schadek wrote:
> On Monday, 7 November 2016 at 23:37:18 UTC, Walter Bright wrote:
>> void callback() nothrow
>> {
>>     scope (failure)
>>     {
>>         ...log error or abort...
>>     }
>>     ...lots of code...
>> }
>
> Who to get the Exception thrown in the scope(failure)

You don't. The exception is also rethrown, so it isn't an exact replacement. (The 'nothrow' is a mistake on my part.)
November 08, 2016
On Tuesday, 8 November 2016 at 01:50:26 UTC, Walter Bright wrote:
> On 11/7/2016 4:12 PM, Robert burner Schadek wrote:
>> On Monday, 7 November 2016 at 23:37:18 UTC, Walter Bright wrote:
>>> void callback() nothrow
>>> {
>>>     scope (failure)
>>>     {
>>>         ...log error or abort...
>>>     }
>>>     ...lots of code...
>>> }
>>
>> Who to get the Exception thrown in the scope(failure)
>
> You don't. The exception is also rethrown, so it isn't an exact replacement. (The 'nothrow' is a mistake on my part.)

In this specific case, a function used as a callback for a C API, it really ought to be nothrow because of the inconsistent behavior with propagating exceptions across language boundaries. scope(exit) just isn't useful here.

I recall a proposal somewhere for something like scope(exit, e), where 'e' is a Throwable instance. That would be great for logging the exception message, but unless it also allowed for aborting the throw, it still wouldn't be useful in this specific case.
November 07, 2016
On 11/6/16 11:30 PM, Manu via Digitalmars-d wrote:
> Hey people, I'm passing lots of D function pointers to C, and naturally,
> the C api expects the fp signatures are all nothrow.
>
> Which means, my D functions all look like this:
>
> void callback() nothrow
> {
> try
> {
> ...lots of code...
> }
> catch (Exception e)
> {
> ...log error or abort...
> }
> }
>
>
> I'm generally annoyed by all the extra indentation.
> Since nothrow is a central thing in D, I wondered if it would be
> reasonable to allow a nice little bit of sugar to assist working with
> nothrow that would look like this:
>
> void catchingCallback() nothrow
> {
> ...lots of code...
> }
> catch (Exception e)
> {
> ...log error or abort...
> }
>
> Syntactically similar to the existing in/out sections, although I think
> it would be nicer at the bottom...
> It's simply a lowering which wraps the entire function body scope in a try.

the tough thing about this is, what is in scope inside the catch? You are outside the function, so the only thing available is the exception.

Being stupid, I thought maybe this might work:

nothrow void catchingCallback() try
{
}
catch(Exception e)
{
}

Thinking "hey, a function body is like a scope, maybe I can just do one statement instead!"

But it doesn't work. However, this isn't far off:

void catchingCallBack() nothrow {try
{
}
catch(Exception e)
{
}}

Another solution may be to write your function as normal, and then mixin definitions for a nothrow version of it. Of course, this means you have to handle all your exceptions the same way.

-Steve
November 08, 2016
On Tuesday, 8 November 2016 at 01:50:26 UTC, Walter Bright wrote:
> You don't. The exception is also rethrown, so it isn't an exact replacement. (The 'nothrow' is a mistake on my part.)

this:

scope(failure, Exception e) {
    // Do something with e
}

would be nice


November 08, 2016
On Tuesday, 8 November 2016 at 01:50:26 UTC, Walter Bright wrote:
>> Who to get the Exception thrown in the scope(failure)
>
> You don't. The exception is also rethrown, so it isn't an exact replacement. (The 'nothrow' is a mistake on my part.)

You can eat the exception by returning in the scope guard. Since it seems to trigger on Throwables it also eats Errors, sadly.

void fun() nothrow
{
    scope(failure)
    {
        // cleanup logic
        return;
    }

    throw new Exception("I don't get rethrown");
}
« First   ‹ Prev
1 2