Trailing catch on function?

Jacob Carlborg via Digitalmars-d digitalmars-d at puremagic.com
Mon Nov 7 12:19:52 PST 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


More information about the Digitalmars-d mailing list