Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

PURGE COMPLETE.


devel / comp.lang.c / Re: try-catch macros

SubjectAuthor
* try-catch macrosThiago Adams
+- Re: try-catch macrosThiago Adams
+* Re: try-catch macrosPaul N
|`* Re: try-catch macrosThiago Adams
| `- Re: try-catch macrosFelix Palmen
`* Re: try-catch macrosThiago Adams
 `* Re: try-catch macrosÖö Tiib
  `- Re: try-catch macrosThiago Adams

1
try-catch macros

<4f0d6144-1a34-4efc-8351-c780a010d2cfn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=25610&group=comp.lang.c#25610

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:4405:b0:757:86e0:b37f with SMTP id v5-20020a05620a440500b0075786e0b37fmr2386477qkp.11.1683720283513;
Wed, 10 May 2023 05:04:43 -0700 (PDT)
X-Received: by 2002:a05:620a:1a03:b0:742:9e15:3e0 with SMTP id
bk3-20020a05620a1a0300b007429e1503e0mr6492117qkb.5.1683720282315; Wed, 10 May
2023 05:04:42 -0700 (PDT)
Path: i2pn2.org!i2pn.org!news.1d4.us!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Wed, 10 May 2023 05:04:42 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=170.231.44.126; posting-account=xFcAQAoAAAAoWlfpQ6Hz2n-MU9fthxbY
NNTP-Posting-Host: 170.231.44.126
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <4f0d6144-1a34-4efc-8351-c780a010d2cfn@googlegroups.com>
Subject: try-catch macros
From: thiago.a...@gmail.com (Thiago Adams)
Injection-Date: Wed, 10 May 2023 12:04:43 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 3219
 by: Thiago Adams - Wed, 10 May 2023 12:04 UTC

The motivation for these macros is to refactor code like:

if (success)
if (success)
if (success)
...

into

try {
if (error) throw;
if (error) throw;
if (error) throw;
...
} catch {
}

Where macros are

#define try if (1)
#define catch else catch_label:
#define throw goto catch_label

When I asked "do you like this macro" the feedback was 100% negative.

After using this macro in a real program with 131 try-catch blocks I
counted the number of empty catch blocks. In 80% of cases
the catch block was empty.

So, basically ,in 80% of cases I can replace the macros just for
goto exit.

if (error) goto exit;
if (error) goto exit;
if (error) goto exit;
....

exit:

Making the macro useless.
(I think this is OK, even using goto)

I created this post to ask about alternatives for this 20% reaming
cases where I actually have used the catch block.

The direct alternative is:

if (error) goto error;
if (error) goto error;

....

if (0) error: {
/*code executed only when error is present*/
}

But it is ugly. Isn't it?

Most usages of catch block were like this:

void list_push(list* list, item)
{ try {
if (error) throw;
if (error) throw;
if (error) throw;
}
catch {
/*we cannot add item into the list*/
free(item);
}
} struct X* load(const char* filename)
{ struct X* p = ..;
try {
if (error) throw;
if (error) throw;
if (error) throw;
}
catch {
free(p);
p = NULL;
}
return p;
} void F(){
try {
if (error) throw;
if (error) throw;
if (error) throw;
}
catch {
log("error at F");
}
}

Alternatives?

void list_push(list* list, item)
{ if (error) goto onerror;
if (error) goto onerror
if (error) goto onerror
...

return;

onerror:
free(item);
} I also find this return ugly, seems to be "lost".

void F(){
try {
if (error) {log("error at F"); goto exit;}
if (error) {log("error at F"); goto exit;}
if (error) {log("error at F"); goto exit;}
}

exit:
}

This is repetitive

Would try-catch macro be a better alternative in this 20%
reaming cases?

Re: try-catch macros

<cf0f4919-d916-4325-bb9b-a2c304562ed8n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=25611&group=comp.lang.c#25611

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:3951:b0:74c:f9b2:47b7 with SMTP id qs17-20020a05620a395100b0074cf9b247b7mr4848292qkn.2.1683720430483;
Wed, 10 May 2023 05:07:10 -0700 (PDT)
X-Received: by 2002:a05:620a:f07:b0:74e:8b1:37eb with SMTP id
v7-20020a05620a0f0700b0074e08b137ebmr5963791qkl.4.1683720430205; Wed, 10 May
2023 05:07:10 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Wed, 10 May 2023 05:07:09 -0700 (PDT)
In-Reply-To: <4f0d6144-1a34-4efc-8351-c780a010d2cfn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=170.231.44.126; posting-account=xFcAQAoAAAAoWlfpQ6Hz2n-MU9fthxbY
NNTP-Posting-Host: 170.231.44.126
References: <4f0d6144-1a34-4efc-8351-c780a010d2cfn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <cf0f4919-d916-4325-bb9b-a2c304562ed8n@googlegroups.com>
Subject: Re: try-catch macros
From: thiago.a...@gmail.com (Thiago Adams)
Injection-Date: Wed, 10 May 2023 12:07:10 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 1527
 by: Thiago Adams - Wed, 10 May 2023 12:07 UTC

On Wednesday, May 10, 2023 at 9:04:51 AM UTC-3, Thiago Adams wrote:
> The motivation for these macros is to refactor code like:
>
> if (success)
> if (success)
> if (success)
> ...
Formatting of the code was lost. Like this
if (success)
{ ..... if (success)
.....{
.........if (success)
.........{
.........}
......}
}

Re: try-catch macros

<d2895049-1151-46f7-8404-0ee443f7571en@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=25612&group=comp.lang.c#25612

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:622a:1982:b0:3ef:3541:435e with SMTP id u2-20020a05622a198200b003ef3541435emr6793267qtc.1.1683724004345;
Wed, 10 May 2023 06:06:44 -0700 (PDT)
X-Received: by 2002:a05:622a:180f:b0:3f4:95b2:b0a4 with SMTP id
t15-20020a05622a180f00b003f495b2b0a4mr434370qtc.10.1683724004109; Wed, 10 May
2023 06:06:44 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Wed, 10 May 2023 06:06:43 -0700 (PDT)
In-Reply-To: <4f0d6144-1a34-4efc-8351-c780a010d2cfn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=88.111.38.19; posting-account=0B-afgoAAABP6274zLUJKa8ZpdIdhsYx
NNTP-Posting-Host: 88.111.38.19
References: <4f0d6144-1a34-4efc-8351-c780a010d2cfn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <d2895049-1151-46f7-8404-0ee443f7571en@googlegroups.com>
Subject: Re: try-catch macros
From: gw7...@aol.com (Paul N)
Injection-Date: Wed, 10 May 2023 13:06:44 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 4101
 by: Paul N - Wed, 10 May 2023 13:06 UTC

On Wednesday, May 10, 2023 at 1:04:51 PM UTC+1, Thiago Adams wrote:
> The motivation for these macros is to refactor code like:
>
> if (success)
> if (success)
> if (success)
> ...
>
> into
>
> try {
> if (error) throw;
> if (error) throw;
> if (error) throw;
> ...
> }
> catch {
> }
>
> Where macros are
>
> #define try if (1)
> #define catch else catch_label:
> #define throw goto catch_label
>
> When I asked "do you like this macro" the feedback was 100% negative.
>
> After using this macro in a real program with 131 try-catch blocks I
> counted the number of empty catch blocks. In 80% of cases
> the catch block was empty.
>
> So, basically ,in 80% of cases I can replace the macros just for
> goto exit.
>
>
> if (error) goto exit;
> if (error) goto exit;
> if (error) goto exit;
> ...
>
> exit:
>
> Making the macro useless.
> (I think this is OK, even using goto)
>
> I created this post to ask about alternatives for this 20% reaming
> cases where I actually have used the catch block.
>
> The direct alternative is:
>
> if (error) goto error;
> if (error) goto error;
>
> ...
>
> if (0) error: {
> /*code executed only when error is present*/
> }
>
> But it is ugly. Isn't it?
>
> Most usages of catch block were like this:
>
> void list_push(list* list, item)
> {
> try {
> if (error) throw;
> if (error) throw;
> if (error) throw;
> }
> catch {
> /*we cannot add item into the list*/
> free(item);
> }
> }
> struct X* load(const char* filename)
> {
> struct X* p = ..;
> try {
> if (error) throw;
> if (error) throw;
> if (error) throw;
> }
> catch {
> free(p);
> p = NULL;
> }
> return p;
> }
> void F(){
> try {
> if (error) throw;
> if (error) throw;
> if (error) throw;
> }
> catch {
> log("error at F");
> }
> }
>
>
>
> Alternatives?
>
> void list_push(list* list, item)
> {
> if (error) goto onerror;
> if (error) goto onerror
> if (error) goto onerror
> ...
>
> return;
>
> onerror:
> free(item);
> }
> I also find this return ugly, seems to be "lost".
>
> void F(){
> try {
> if (error) {log("error at F"); goto exit;}
> if (error) {log("error at F"); goto exit;}
> if (error) {log("error at F"); goto exit;}
> }
>
> exit:
> }
>
> This is repetitive
>
> Would try-catch macro be a better alternative in this 20%
> reaming cases?

How about something like:

if (error) goto exit;
if (error) goto exit;
if (this_error) goto handle_error;

....

handle_error:

// stuff here

exit:

Re: try-catch macros

<1eb95834-2403-49a7-8635-8c03c86ef32cn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=25613&group=comp.lang.c#25613

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:1a03:b0:742:9e15:3e0 with SMTP id bk3-20020a05620a1a0300b007429e1503e0mr6598845qkb.5.1683727981319;
Wed, 10 May 2023 07:13:01 -0700 (PDT)
X-Received: by 2002:a05:620a:3715:b0:74e:324:d6f0 with SMTP id
de21-20020a05620a371500b0074e0324d6f0mr4954891qkb.7.1683727981020; Wed, 10
May 2023 07:13:01 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!feeder1.feed.usenet.farm!feed.usenet.farm!peer03.ams4!peer.am4.highwinds-media.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Wed, 10 May 2023 07:13:00 -0700 (PDT)
In-Reply-To: <d2895049-1151-46f7-8404-0ee443f7571en@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=170.231.44.126; posting-account=xFcAQAoAAAAoWlfpQ6Hz2n-MU9fthxbY
NNTP-Posting-Host: 170.231.44.126
References: <4f0d6144-1a34-4efc-8351-c780a010d2cfn@googlegroups.com> <d2895049-1151-46f7-8404-0ee443f7571en@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <1eb95834-2403-49a7-8635-8c03c86ef32cn@googlegroups.com>
Subject: Re: try-catch macros
From: thiago.a...@gmail.com (Thiago Adams)
Injection-Date: Wed, 10 May 2023 14:13:01 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 2031
 by: Thiago Adams - Wed, 10 May 2023 14:13 UTC

On Wednesday, May 10, 2023 at 10:06:52 AM UTC-3, Paul N wrote:
....
> > Would try-catch macro be a better alternative in this 20%
> > reaming cases?
> How about something like:
> if (error) goto exit;
> if (error) goto exit;
> if (this_error) goto handle_error;
>
> ...
>
> handle_error:
>
> // stuff here
>
> exit:

Not sure if I understood. but in any error I want to use the same
error routine that is executed only if some error happens.
But you gave another idea.

if (error) goto error;
if (error) goto error
if (error) goto error
/*sucess*/
goto exit;

error:
...
exit:
....

that is a variation of return instead of goto exit;

Re: try-catch macros

<828b9396-2840-47f6-a9ac-ce37b3b01078n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=25614&group=comp.lang.c#25614

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ad4:4f33:0:b0:621:3617:caa3 with SMTP id fc19-20020ad44f33000000b006213617caa3mr1001447qvb.9.1683728983731;
Wed, 10 May 2023 07:29:43 -0700 (PDT)
X-Received: by 2002:a05:622a:48:b0:3f3:8819:67de with SMTP id
y8-20020a05622a004800b003f3881967demr5092781qtw.8.1683728983440; Wed, 10 May
2023 07:29:43 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!feeder1.feed.usenet.farm!feed.usenet.farm!peer01.ams4!peer.am4.highwinds-media.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Wed, 10 May 2023 07:29:43 -0700 (PDT)
In-Reply-To: <4f0d6144-1a34-4efc-8351-c780a010d2cfn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=170.231.44.126; posting-account=xFcAQAoAAAAoWlfpQ6Hz2n-MU9fthxbY
NNTP-Posting-Host: 170.231.44.126
References: <4f0d6144-1a34-4efc-8351-c780a010d2cfn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <828b9396-2840-47f6-a9ac-ce37b3b01078n@googlegroups.com>
Subject: Re: try-catch macros
From: thiago.a...@gmail.com (Thiago Adams)
Injection-Date: Wed, 10 May 2023 14:29:43 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 1457
 by: Thiago Adams - Wed, 10 May 2023 14:29 UTC

On Wednesday, May 10, 2023 at 9:04:51 AM UTC-3, Thiago Adams wrote:
> Would try-catch macro be a better alternative in this 20%
> reaming cases?

Typos
reaming

should be

remaining

sorry

Re: try-catch macros

<537dd81f-b993-42ef-b29d-dd2eab8ab9e8n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=25617&group=comp.lang.c#25617

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ad4:48d2:0:b0:61a:2d8a:e7d3 with SMTP id v18-20020ad448d2000000b0061a2d8ae7d3mr3994615qvx.9.1683827476127;
Thu, 11 May 2023 10:51:16 -0700 (PDT)
X-Received: by 2002:a05:622a:1dcf:b0:3f4:e756:85a7 with SMTP id
bn15-20020a05622a1dcf00b003f4e75685a7mr524105qtb.1.1683827475984; Thu, 11 May
2023 10:51:15 -0700 (PDT)
Path: i2pn2.org!i2pn.org!news.swapon.de!news.mixmin.net!newsreader4.netcologne.de!news.netcologne.de!peer01.ams1!peer.ams1.xlned.com!news.xlned.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Thu, 11 May 2023 10:51:15 -0700 (PDT)
In-Reply-To: <828b9396-2840-47f6-a9ac-ce37b3b01078n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=84.50.190.130; posting-account=pysjKgkAAACLegAdYDFznkqjgx_7vlUK
NNTP-Posting-Host: 84.50.190.130
References: <4f0d6144-1a34-4efc-8351-c780a010d2cfn@googlegroups.com> <828b9396-2840-47f6-a9ac-ce37b3b01078n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <537dd81f-b993-42ef-b29d-dd2eab8ab9e8n@googlegroups.com>
Subject: Re: try-catch macros
From: oot...@hot.ee (Öö Tiib)
Injection-Date: Thu, 11 May 2023 17:51:16 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 2595
 by: Öö Tiib - Thu, 11 May 2023 17:51 UTC

On Wednesday, 10 May 2023 at 17:29:51 UTC+3, Thiago Adams wrote:
> On Wednesday, May 10, 2023 at 9:04:51 AM UTC-3, Thiago Adams wrote:
> > Would try-catch macro be a better alternative in this 20%
> > reaming cases?
> Typos
> reaming
>
> should be
>
> remaining
>
> sorry

The throw and catch are bad replacement for if and
else (and are also bad replacement to goto and label). The if
and else (and very occasionally goto and label) are lot easier
tools for branching within a function.

Idea of throw is to give up whole operation regardless of
how deep in call stack you figured that there is show stopper
obstacle to its completion. Idea of catch is to then handle the
situation by handling it. Handling is typically rolling everything
back to situation before start of operation , reporting
it and giving up or trying to fix whatever caused the show
stopper and retrying the operation, or trying some less fragile
alternative.

You talk about empty catch blocks. So catching without handling?
I do not know who writes that kind of code and what is the point
of it. Unreliable software that occasionally fails but does nothing
about it and results with incorrect situation in unpredictable
ways and for unknown reasons?

Re: try-catch macros

<7rnvij-6631.ln1@mail.home.palmen-it.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=25618&group=comp.lang.c#25618

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.swapon.de!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: fel...@palmen-it.de (Felix Palmen)
Newsgroups: comp.lang.c
Subject: Re: try-catch macros
Date: Thu, 11 May 2023 20:13:27 +0200
Organization: palmen-it.de
Lines: 49
Message-ID: <7rnvij-6631.ln1@mail.home.palmen-it.de>
References: <4f0d6144-1a34-4efc-8351-c780a010d2cfn@googlegroups.com> <d2895049-1151-46f7-8404-0ee443f7571en@googlegroups.com> <1eb95834-2403-49a7-8635-8c03c86ef32cn@googlegroups.com>
Injection-Date: Thu, 11 May 2023 20:13:27 +0200
Injection-Info: dont-email.me; posting-host="496c2b1f7627cb4593860c05a4bfc126";
logging-data="1242119"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/5WKCA+DwxL+aepEvamKBC"
User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (FreeBSD/13.2-RELEASE (amd64)) tinews.pl/1.1.61
Cancel-Lock: sha256:wSkYoGncQzU8t8YVKzp3UaFqf7aEBSeQRR/CDD/Hmz4=
sha1:z7n10H9XwOsNMXFWiSSxP5NZJa8=
X-PGP-Sig: GnuPG-v2 From,Newsgroups,Subject,Date,Injection-Date,Message-ID
iNUEARYIAH0WIQRpNhPVW79IN7ISOsxUreAGmHnyMQUCZF0wSV8UgAAAAAAuAChp
c3N1ZXItZnByQG5vdGF0aW9ucy5vcGVucGdwLmZpZnRoaG9yc2VtYW4ubmV0Njkz
NjEzRDU1QkJGNDgzN0IyMTIzQUNDNTRBREUwMDY5ODc5RjIzMQAKCRBUreAGmHny
MeSZAQCy1bD0j+3C2HlE0EoeJyvMgNLPsQDWSGJNoGHgSssvgQD/SuMHg6YHe+W/
Ak9vpTCz63kfBa4dxNJ49jN/punccws=
=ZfYJ
X-PGP-Key: 693613D55BBF4837B2123ACC54ADE0069879F231
X-PGP-Hash: SHA256
 by: Felix Palmen - Thu, 11 May 2023 18:13 UTC

* Thiago Adams <thiago.adams@gmail.com>:
> /*sucess*/
> goto exit;

This should almost always be unnecessary if you both keep your functions
small (one aspect, one function) and initialized any variable used for
resources. If you want to add e.g. some logging, you could even add e.g.
some variable for that. Something like:

#v+
{ File *file = 0;
void *hdl = 0;
const char *errstr = 0;

/* do stuff */

if (!(file = fopen("foobar", "r"))
{
errstr = "error opening foobar";
goto done;
}

/* do more stuff */

done:
if (errstr)
{
Log(errstr);
if (file) fclose(file);
if (hdl) destroy(hdl);
return -1;
}
return 0;
} #v-

And if you don't need/want logging, some 'int rc' initialized to -1 and
changed to 0 on success comes handy.

IMHO, really keep "exceptions" out, even more so if just mimicking them
(poorly) ... a real exception would unroll the stack, you'd need
setjmp()/longjmp() to get somewhat close to that.

--
Dipl.-Inform. Felix Palmen <felix@palmen-it.de> ,.//..........
{web} http://palmen-it.de {jabber} [see email] ,//palmen-it.de
{pgp public key} http://palmen-it.de/pub.txt // """""""""""
{pgp fingerprint} 6936 13D5 5BBF 4837 B212 3ACC 54AD E006 9879 F231

Re: try-catch macros

<3729eca7-9f85-453a-a356-993ee0ea4880n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=25621&group=comp.lang.c#25621

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ac8:578d:0:b0:3f3:71c6:4edd with SMTP id v13-20020ac8578d000000b003f371c64eddmr8816896qta.3.1683894238289;
Fri, 12 May 2023 05:23:58 -0700 (PDT)
X-Received: by 2002:a05:620a:f07:b0:74e:8b1:37eb with SMTP id
v7-20020a05620a0f0700b0074e08b137ebmr8176218qkl.4.1683894237943; Fri, 12 May
2023 05:23:57 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!1.us.feeder.erje.net!feeder.erje.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Fri, 12 May 2023 05:23:57 -0700 (PDT)
In-Reply-To: <537dd81f-b993-42ef-b29d-dd2eab8ab9e8n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=170.231.44.126; posting-account=xFcAQAoAAAAoWlfpQ6Hz2n-MU9fthxbY
NNTP-Posting-Host: 170.231.44.126
References: <4f0d6144-1a34-4efc-8351-c780a010d2cfn@googlegroups.com>
<828b9396-2840-47f6-a9ac-ce37b3b01078n@googlegroups.com> <537dd81f-b993-42ef-b29d-dd2eab8ab9e8n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <3729eca7-9f85-453a-a356-993ee0ea4880n@googlegroups.com>
Subject: Re: try-catch macros
From: thiago.a...@gmail.com (Thiago Adams)
Injection-Date: Fri, 12 May 2023 12:23:58 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 5084
 by: Thiago Adams - Fri, 12 May 2023 12:23 UTC

On Thursday, May 11, 2023 at 2:51:23 PM UTC-3, Öö Tiib wrote:
> On Wednesday, 10 May 2023 at 17:29:51 UTC+3, Thiago Adams wrote:
> > On Wednesday, May 10, 2023 at 9:04:51 AM UTC-3, Thiago Adams wrote:
> > > Would try-catch macro be a better alternative in this 20%
> > > reaming cases?
> > Typos
> > reaming
> >
> > should be
> >
> > remaining
> >
> > sorry
> The throw and catch are bad replacement for if and
> else (and are also bad replacement to goto and label). The if
> and else (and very occasionally goto and label) are lot easier
> tools for branching within a function.
>
> Idea of throw is to give up whole operation regardless of
> how deep in call stack you figured that there is show stopper
> obstacle to its completion. Idea of catch is to then handle the
> situation by handling it. Handling is typically rolling everything
> back to situation before start of operation , reporting
> it and giving up or trying to fix whatever caused the show
> stopper and retrying the operation, or trying some less fragile
> alternative.


The idea is local jumps only (not as a limitation but as a desired feature),
The names try-catch were chosen because I could find better names.

>
> You talk about empty catch blocks. So catching without handling?
> I do not know who writes that kind of code and what is the point
> of it. Unreliable software that occasionally fails but does nothing
> about it and results with incorrect situation in unpredictable
> ways and for unknown reasons?

catch block is a region of code that is executed only if we
throw (jump into it – name is bad)

The reason 80% had empty catch blocks is because throw was used only to
jump out of try-block, but I didn't need to execute anything extra
on catch block.

So, in this case the feature is similar of just using

if (error) goto exit;
if (error) goto exit;
if (error) goto exit;
exit:

But (I am again) considering an advantage of try blocks.
Because generally, the pattern I use is:

struct Resource * p1 = 0;
struct Resource* p2 = 0;
try {
if (error) throw;
if (error) throw;
} catch{}
ReleaseResource(p1);
ReleaseResource(p2);

Becase I release resources outside the try block I need to declare
out of the try block. Initially I thought this was a disadvantage
because I cannot declare and initialize at the same line.

But later I realized if we jump we need to make sure all variables are
initialized with empty states.
I cannot do this for instance:

struct Resource * p1 = ...;
if (error) goto exit;

struct Resource* p2 = ...;
if (error) goto exit;

exit:

ReleaseResource(p1);

ReleaseResource(p2);


Because p2 would be uninitialized.
The workaround is creating states
struct Resource * p1 = ...;
if (error) goto clear_p1;

struct Resource* p2 = ...;
if (error) goto clear_p2_and_p1;

clear_p2_and_p1:
ReleaseResource(p2);

clear_p1:
ReleaseResource(p1);

But this kind of code is a source of bugs in case we have 3 or
more states.

The other reason I found a advantage of try is very simple, it
creates a identation with the block and to see the destination of
throw is just following the identation. (IDEs also can close blocks)
and compared with goto the is no indentation. Unless..of course we did

.....int *p = null;

.........if (error) goto exit;
.........if (error) goto exit;
.........if (error) goto exit;

.....exit:

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor