Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

OK, enough hype. -- Larry Wall in the perl man page


devel / comp.lang.c / Re: try statement

SubjectAuthor
* Re: try statementThiago Adams
+* Re: try statementÖö Tiib
|`* Re: try statementThiago Adams
| `- Re: try statementÖö Tiib
`* Re: try statementThiago Adams
 +- Re: try statementThiago Adams
 `* Re: try statementKaz Kylheku
  `* Re: try statementThiago Adams
   `* Re: try statementBen Bacarisse
    `* Re: try statementThiago Adams
     `* Re: try statementBen Bacarisse
      `* Re: try statementThiago Adams
       `* Re: try statementBen Bacarisse
        `* Re: try statementThiago Adams
         +- Re: try statementThiago Adams
         `* Re: try statementBen Bacarisse
          `* Re: try statementBart
           +- Re: try statementBen Bacarisse
           `* Re: try statementThiago Adams
            `* Re: try statementBart
             `- Re: try statementThiago Adams

1
Re: try statement

<e2bcf7f9-75b8-4775-985b-005f6f3d106fn@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ad4:4947:: with SMTP id o7mr25557866qvy.18.1619556294606;
Tue, 27 Apr 2021 13:44:54 -0700 (PDT)
X-Received: by 2002:a37:9206:: with SMTP id u6mr15047975qkd.7.1619556294359;
Tue, 27 Apr 2021 13:44:54 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Tue, 27 Apr 2021 13:44:54 -0700 (PDT)
In-Reply-To: <50e63b3d-5937-479b-bcf6-529f514a4becn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=189.6.254.153; posting-account=xFcAQAoAAAAoWlfpQ6Hz2n-MU9fthxbY
NNTP-Posting-Host: 189.6.254.153
References: <f5a816bb-9786-4c68-93ae-188bf8a58dd7n@googlegroups.com>
<s5i8kg$fjv$1@gioia.aioe.org> <bfa279e9-800d-4e66-8bd6-8faf1ea68f57n@googlegroups.com>
<543fe06a-9746-416c-a7b9-fe354dbb09bdn@googlegroups.com> <50e63b3d-5937-479b-bcf6-529f514a4becn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <e2bcf7f9-75b8-4775-985b-005f6f3d106fn@googlegroups.com>
Subject: Re: try statement
From: thiago.a...@gmail.com (Thiago Adams)
Injection-Date: Tue, 27 Apr 2021 20:44:54 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
 by: Thiago Adams - Tue, 27 Apr 2021 20:44 UTC

On Thursday, April 22, 2021 at 6:16:55 PM UTC-3, Thiago Adams wrote:
> I included try-blocks and throw with a local jump.

Some samples:

try {
/*maybe inside nested loops*/
/*this is just a goto to the end of try block*/
throw;
} /*end of try block*/

try {
/*this is just a goto to inside catch block
throw; ​
} catch {
/*inside catch block*/
}

/*creates a variable 'int er' as defined in catch*/
try {
throw 1; /*sets the variable to 1 then jump*/
} catch (int er) {
/*inside catch block*/
}

try {
int i = 0;
try (i == 0);

/*same as if (!(i == 0)) throw;*/
}

try {
int i = 0;
try (i == 0) throw 2;

/*same as if (!(i == 0)) throw 2;*/
} catch (int er) {
}

try {
/*
Resource acquisition is initialization (RAII).
f checks the initialization
fclose is the dtor that is called at the end of scope
*/
try (FILE* f = fopen("t.txt", "r"); f; fclose(f)) throw errno;
} catch (int er) {
}

Re: try statement

<228deec4-7b30-4347-80ee-cb273eff8fe0n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ad4:59c7:: with SMTP id el7mr19963874qvb.26.1619583599231;
Tue, 27 Apr 2021 21:19:59 -0700 (PDT)
X-Received: by 2002:a0c:b303:: with SMTP id s3mr26864766qve.22.1619583599113;
Tue, 27 Apr 2021 21:19:59 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Tue, 27 Apr 2021 21:19:58 -0700 (PDT)
In-Reply-To: <e2bcf7f9-75b8-4775-985b-005f6f3d106fn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=94.246.251.86; posting-account=pysjKgkAAACLegAdYDFznkqjgx_7vlUK
NNTP-Posting-Host: 94.246.251.86
References: <f5a816bb-9786-4c68-93ae-188bf8a58dd7n@googlegroups.com>
<s5i8kg$fjv$1@gioia.aioe.org> <bfa279e9-800d-4e66-8bd6-8faf1ea68f57n@googlegroups.com>
<543fe06a-9746-416c-a7b9-fe354dbb09bdn@googlegroups.com> <50e63b3d-5937-479b-bcf6-529f514a4becn@googlegroups.com>
<e2bcf7f9-75b8-4775-985b-005f6f3d106fn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <228deec4-7b30-4347-80ee-cb273eff8fe0n@googlegroups.com>
Subject: Re: try statement
From: oot...@hot.ee (Öö Tiib)
Injection-Date: Wed, 28 Apr 2021 04:19:59 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Öö Tiib - Wed, 28 Apr 2021 04:19 UTC

On Tuesday, 27 April 2021 at 23:45:01 UTC+3, Thiago Adams wrote:
> On Thursday, April 22, 2021 at 6:16:55 PM UTC-3, Thiago Adams wrote:
> > I included try-blocks and throw with a local jump.
> Some samples:

Main use-case of exception usage is propagating information about
show-stopper issue down the calling stack without special code
needed in mid way functions.

For example we have function main() that asks file name from user and then
tries to call parse_file() that calls parse_list() that calls parse_record()
that calls parse_property() that throws exception about issue of parsing
the property. Then the exception breaks execution of all those
functions, is caught in main that then says to user that the supplied file
can't be parsed because it contains such issue.

So you seem to use syntax of feature that is good for something
with limitations that make it not useful for what the original is
good for. If so then that confuses initially and then disappoints,
no much fans won.

Re: try statement

<653366b3-741a-4636-a4ac-38b2d3105655n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:1233:: with SMTP id v19mr5551114qkj.418.1619609932660; Wed, 28 Apr 2021 04:38:52 -0700 (PDT)
X-Received: by 2002:ac8:67d3:: with SMTP id r19mr25866550qtp.23.1619609932446; Wed, 28 Apr 2021 04:38:52 -0700 (PDT)
Path: i2pn2.org!i2pn.org!aioe.org!news.uzoreto.com!tr1.eu1.usenetexpress.com!feeder.usenetexpress.com!tr3.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Wed, 28 Apr 2021 04:38:52 -0700 (PDT)
In-Reply-To: <228deec4-7b30-4347-80ee-cb273eff8fe0n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=189.6.254.153; posting-account=xFcAQAoAAAAoWlfpQ6Hz2n-MU9fthxbY
NNTP-Posting-Host: 189.6.254.153
References: <f5a816bb-9786-4c68-93ae-188bf8a58dd7n@googlegroups.com> <s5i8kg$fjv$1@gioia.aioe.org> <bfa279e9-800d-4e66-8bd6-8faf1ea68f57n@googlegroups.com> <543fe06a-9746-416c-a7b9-fe354dbb09bdn@googlegroups.com> <50e63b3d-5937-479b-bcf6-529f514a4becn@googlegroups.com> <e2bcf7f9-75b8-4775-985b-005f6f3d106fn@googlegroups.com> <228deec4-7b30-4347-80ee-cb273eff8fe0n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <653366b3-741a-4636-a4ac-38b2d3105655n@googlegroups.com>
Subject: Re: try statement
From: thiago.a...@gmail.com (Thiago Adams)
Injection-Date: Wed, 28 Apr 2021 11:38:52 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 111
 by: Thiago Adams - Wed, 28 Apr 2021 11:38 UTC

On Wednesday, April 28, 2021 at 1:20:06 AM UTC-3, Öö Tiib wrote:
> On Tuesday, 27 April 2021 at 23:45:01 UTC+3, Thiago Adams wrote:
> > On Thursday, April 22, 2021 at 6:16:55 PM UTC-3, Thiago Adams wrote:
> > > I included try-blocks and throw with a local jump.
> > Some samples:
> Main use-case of exception usage is propagating information about
> show-stopper issue down the calling stack without special code
> needed in mid way functions.

I think a full error propagation can be done with out-arguments and a simple
propagation returning ints.

> For example we have function main() that asks file name from user and then
> tries to call parse_file() that calls parse_list() that calls parse_record()
> that calls parse_property() that throws exception about issue of parsing
> the property. Then the exception breaks execution of all those
> functions, is caught in main that then says to user that the supplied file
> can't be parsed because it contains such issue.

In this sample, I would write just.

struct parse_error {
int code;
/*message line col etc*/
};

int parse_record(struct parse_error* error) {

if ( /some error*/)
seterror(error, "Error parsing at..");

return error->code;
}

int parse_file(const char* file, struct parse_error* error) {
parse_list(error);
return error->code;
}

int main(int argc, char *argv[]) {
struct parse_error error = {0};
parse_file(argv[0], &error);
if (error->code) {
/*print error*/
}
}

I haven't used any "try " feature. This was on purpose
to show that things start to become useful after 2
try items.

For instance:

int parse_file(const char* file, struct parse_error* error) {

try {
try(parse_list(error) == 0) ;
try(parse_list2(error) == 0) ;
try(parse_list3(error) == 0) ;
try(parse_list4(error) == 0) ;
}
return error->code;
}

Note that I don't need to change the main function.

The "defer" (not used in this sample) also become interesting in
to UNDO some resource that was acquired and after error should be
discarded.

Note that our parse_file function has on its signature
the type of error it propagates. The type is checked at
compile time. (Comparing with C++ exceptions
if you throw a new type the caller does not need to change and
this can be dangerous)
How the error is propagated is not specified and different and
existing errors can be mixed.

> So you seem to use syntax of feature that is good for something
> with limitations that make it not useful for what the original is
> good for. If so then that confuses initially and then disappoints,
> no much fans won.

Yes...it is confuse but I guess still the best option because
at least try catch is know pattern.

When I am coding I compare the current implementation (in C)
with this alternative.

In the past I did some code like this:

parse_list(error);
parse_list2(error) ;
parse_list3(error);
parse_list4(error) ;

where I added in each parse function a check
int parse_()
{ if (error->code != 0) return error->code;
....
} then I did not add "ifs" and I let the "zig zag"
flow.

Re: try statement

<55f0842b-a70f-406f-a20f-808fd3b21bcbn@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:21d9:: with SMTP id h25mr28602319qka.70.1619620677078;
Wed, 28 Apr 2021 07:37:57 -0700 (PDT)
X-Received: by 2002:a05:622a:245:: with SMTP id c5mr17765963qtx.350.1619620676890;
Wed, 28 Apr 2021 07:37:56 -0700 (PDT)
Path: i2pn2.org!i2pn.org!paganini.bofh.team!usenet.pasdenom.info!usenet-fr.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Wed, 28 Apr 2021 07:37:56 -0700 (PDT)
In-Reply-To: <653366b3-741a-4636-a4ac-38b2d3105655n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=94.246.251.86; posting-account=pysjKgkAAACLegAdYDFznkqjgx_7vlUK
NNTP-Posting-Host: 94.246.251.86
References: <f5a816bb-9786-4c68-93ae-188bf8a58dd7n@googlegroups.com>
<s5i8kg$fjv$1@gioia.aioe.org> <bfa279e9-800d-4e66-8bd6-8faf1ea68f57n@googlegroups.com>
<543fe06a-9746-416c-a7b9-fe354dbb09bdn@googlegroups.com> <50e63b3d-5937-479b-bcf6-529f514a4becn@googlegroups.com>
<e2bcf7f9-75b8-4775-985b-005f6f3d106fn@googlegroups.com> <228deec4-7b30-4347-80ee-cb273eff8fe0n@googlegroups.com>
<653366b3-741a-4636-a4ac-38b2d3105655n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <55f0842b-a70f-406f-a20f-808fd3b21bcbn@googlegroups.com>
Subject: Re: try statement
From: oot...@hot.ee (Öö Tiib)
Injection-Date: Wed, 28 Apr 2021 14:37:57 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
 by: Öö Tiib - Wed, 28 Apr 2021 14:37 UTC

On Wednesday, 28 April 2021 at 14:39:01 UTC+3, Thiago Adams wrote:
> On Wednesday, April 28, 2021 at 1:20:06 AM UTC-3, Öö Tiib wrote:
> > On Tuesday, 27 April 2021 at 23:45:01 UTC+3, Thiago Adams wrote:
> > > On Thursday, April 22, 2021 at 6:16:55 PM UTC-3, Thiago Adams wrote:
> > > > I included try-blocks and throw with a local jump.
> > > Some samples:
> > Main use-case of exception usage is propagating information about
> > show-stopper issue down the calling stack without special code
> > needed in mid way functions.
> I think a full error propagation can be done with out-arguments and a simple
> propagation returning ints.

Error handling can and often is done with explicit handling of out arguments
and/or return values. But the whole point of exceptions is to move that
exceptional code from algorithm code to error handling blocks.
Otherwise if functions called can fail we can't write readable algorithms like:

acceleration = velocity(object, end_time) - velocity(object. start_time)
/ (end_time - start_time) ;

We have to check if call to velocity failed right after call and that can't be
done in middle of expression.

The C++ proposal of static exceptions to what you gave link else thread is
alternative to current table-based stack unwinding, but is not changing
syntax of exceptions usage. It is performance-wise better fit for real time
systems and the point of mixing exceptions (with rarely occurring problems)
and manual error processing (with frequently occurring problems) is also
gone. Great proposal, but it is really more about ABI (under control of
monsters) and compiler (also under control of monsters) so it won't ever
happen.

It just does it internally differently than current ... roughly like that:
* The return value of noexcept(false) functions is turned into kind of tagged
union between return value type (if any) and all potentially thrown exception
information types behind scenes, programmer does need to type nothing
for that.
* The noexcept(true) functions call terminate() instead of returning exception
information.
* Caller of noexcept(false) functions checks return value of each call if it is
exception behind scenes.
* * On case it is not it runs on.
* * On case it is exception and code is not in try block it returns exception
information.
* * On case it is exception and code is in try block it searches for matching
catch first to last.
* * * If catch found then it jumps into that catch, catch(...) matches everything.
* * * If none of the catches match then return exception information.
* same logic is for explicit throw with argument.
* throw; without argument rethrows last exception.
* etc more of such details

> > For example we have function main() that asks file name from user and then
> > tries to call parse_file() that calls parse_list() that calls parse_record()
> > that calls parse_property() that throws exception about issue of parsing
> > the property. Then the exception breaks execution of all those
> > functions, is caught in main that then says to user that the supplied file
> > can't be parsed because it contains such issue.

Yes that misses whole point; idea of programming language features is to
make code more coherent, expressive and better structured not to add more
need of bloat to type or ways to type same bloat more verbosely.
Exceptions are such feature that has been here for decades and so people
expect something of those, bait-and-switch with those won't work.

> > So you seem to use syntax of feature that is good for something
> > with limitations that make it not useful for what the original is
> > good for. If so then that confuses initially and then disappoints,
> > no much fans won.
> Yes...it is confuse but I guess still the best option because
> at least try catch is know pattern.

It is, that's why I say that your "try" promises something, then confuses
"wtf is that" and finally disappoints "oh its not there, useless". Good
way to align market against you.

If you have windows box somewhere then download ms compiler
and turn on SEH exceptions in it.
<https://docs.microsoft.com/en-us/cpp/cpp/structured-exception-handling-c-cpp?view=msvc-160>
These are not that fun in C as C does not have RAII but at least useful
for having exception support in language so these do not disappoint
that badly. Making anything less convenient than those won't fly.
Combining with some trick to add RAII to C would likely feel great.

Re: try statement

<3f43d338-65ba-4ef4-a584-9409f173164en@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a0c:e8c4:: with SMTP id m4mr3044023qvo.21.1619751752964;
Thu, 29 Apr 2021 20:02:32 -0700 (PDT)
X-Received: by 2002:a0c:9bda:: with SMTP id g26mr3016014qvf.44.1619751752676;
Thu, 29 Apr 2021 20:02:32 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Thu, 29 Apr 2021 20:02:32 -0700 (PDT)
In-Reply-To: <e2bcf7f9-75b8-4775-985b-005f6f3d106fn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=189.6.254.153; posting-account=xFcAQAoAAAAoWlfpQ6Hz2n-MU9fthxbY
NNTP-Posting-Host: 189.6.254.153
References: <f5a816bb-9786-4c68-93ae-188bf8a58dd7n@googlegroups.com>
<s5i8kg$fjv$1@gioia.aioe.org> <bfa279e9-800d-4e66-8bd6-8faf1ea68f57n@googlegroups.com>
<543fe06a-9746-416c-a7b9-fe354dbb09bdn@googlegroups.com> <50e63b3d-5937-479b-bcf6-529f514a4becn@googlegroups.com>
<e2bcf7f9-75b8-4775-985b-005f6f3d106fn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <3f43d338-65ba-4ef4-a584-9409f173164en@googlegroups.com>
Subject: Re: try statement
From: thiago.a...@gmail.com (Thiago Adams)
Injection-Date: Fri, 30 Apr 2021 03:02:32 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
 by: Thiago Adams - Fri, 30 Apr 2021 03:02 UTC

On Tuesday, April 27, 2021 at 5:45:01 PM UTC-3, Thiago Adams wrote:
> On Thursday, April 22, 2021 at 6:16:55 PM UTC-3, Thiago Adams wrote:
> > I included try-blocks and throw with a local jump.
> Some samples:
....
> try {
> /*this is just a goto to inside catch block
> throw; ​
> }
> catch {
> /*inside catch block*/
> }

Emulation of this particular case with macros.

#define try int _try_block_error = 0; do
#define catch while(0); catch_label:; if (_try_block_error)
#define throw do { _try_block_error = 1; goto catch_label; } while (0)

int main() {

try
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (j == 1 && i == 5)
throw;
}
}
printf("continue..\n");
}
catch {
printf("catch\n");
}

}

Re: try statement

<d85d63b1-49d5-4956-adf7-3e74ba8f496bn@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a0c:aacd:: with SMTP id g13mr3357847qvb.41.1619759095344;
Thu, 29 Apr 2021 22:04:55 -0700 (PDT)
X-Received: by 2002:a37:aec6:: with SMTP id x189mr3625611qke.348.1619759095076;
Thu, 29 Apr 2021 22:04:55 -0700 (PDT)
Path: i2pn2.org!rocksolid2!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Thu, 29 Apr 2021 22:04:54 -0700 (PDT)
In-Reply-To: <3f43d338-65ba-4ef4-a584-9409f173164en@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=189.6.254.153; posting-account=xFcAQAoAAAAoWlfpQ6Hz2n-MU9fthxbY
NNTP-Posting-Host: 189.6.254.153
References: <f5a816bb-9786-4c68-93ae-188bf8a58dd7n@googlegroups.com>
<s5i8kg$fjv$1@gioia.aioe.org> <bfa279e9-800d-4e66-8bd6-8faf1ea68f57n@googlegroups.com>
<543fe06a-9746-416c-a7b9-fe354dbb09bdn@googlegroups.com> <50e63b3d-5937-479b-bcf6-529f514a4becn@googlegroups.com>
<e2bcf7f9-75b8-4775-985b-005f6f3d106fn@googlegroups.com> <3f43d338-65ba-4ef4-a584-9409f173164en@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <d85d63b1-49d5-4956-adf7-3e74ba8f496bn@googlegroups.com>
Subject: Re: try statement
From: thiago.a...@gmail.com (Thiago Adams)
Injection-Date: Fri, 30 Apr 2021 05:04:55 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Thiago Adams - Fri, 30 Apr 2021 05:04 UTC

On Friday, April 30, 2021 at 12:02:39 AM UTC-3, Thiago Adams wrote:
....
> Emulation of this particular case with macros.
>
>
> #define try int _try_block_error = 0; do
> #define catch while(0); catch_label:; if (_try_block_error)
> #define throw do { _try_block_error = 1; goto catch_label; } while (0)
>
[...]

I didn't know that we could use goto labels between else and { } !
and the code execution continues inside the else.

So, this is the alternative emulation.

#include <stdio.h>

#define try if (1)
#define catch else catch_label:
#define throw do { goto catch_label; } while (0)

int main()
{

try
{
throw;
}
catch
{
printf("catch\n");
}
printf("continue\n");

}

Re: try statement

<20210429224042.891@kylheku.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: 563-365-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: try statement
Date: Fri, 30 Apr 2021 05:43:59 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 73
Message-ID: <20210429224042.891@kylheku.com>
References: <f5a816bb-9786-4c68-93ae-188bf8a58dd7n@googlegroups.com>
<s5i8kg$fjv$1@gioia.aioe.org>
<bfa279e9-800d-4e66-8bd6-8faf1ea68f57n@googlegroups.com>
<543fe06a-9746-416c-a7b9-fe354dbb09bdn@googlegroups.com>
<50e63b3d-5937-479b-bcf6-529f514a4becn@googlegroups.com>
<e2bcf7f9-75b8-4775-985b-005f6f3d106fn@googlegroups.com>
<3f43d338-65ba-4ef4-a584-9409f173164en@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 30 Apr 2021 05:43:59 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="62ce02e6b5cf78fa9608b216ae50b6a5";
logging-data="7447"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/03yfcLhZv8Rc1bK34nzc7o3SkSIuvVA4="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:yiJZC1oAMUOOlGFxMpFPI3zgKKc=
 by: Kaz Kylheku - Fri, 30 Apr 2021 05:43 UTC

On 2021-04-30, Thiago Adams <thiago.adams@gmail.com> wrote:
> On Tuesday, April 27, 2021 at 5:45:01 PM UTC-3, Thiago Adams wrote:
>> On Thursday, April 22, 2021 at 6:16:55 PM UTC-3, Thiago Adams wrote:
>> > I included try-blocks and throw with a local jump.
>> Some samples:
> ...
>> try {
>> /*this is just a goto to inside catch block
>> throw; ​
>> }
>> catch {
>> /*inside catch block*/
>> }
>
> Emulation of this particular case with macros.
>
>
> #define try int _try_block_error = 0; do
> #define catch while(0); catch_label:; if (_try_block_error)
> #define throw do { _try_block_error = 1; goto catch_label; } while (0)
>
>
> int main() {
>
> try
> {
> for (int i = 0; i < 10; i++)
> {
> for (int j = 0; j < 10; j++)
> {
> if (j == 1 && i == 5)
> throw;
> }
> }
> printf("continue..\n");
> }
> catch {
> printf("catch\n");
> }
>
> }

Without the following support, through any number of nestings, it's
pretty silly:

try
{
unwind_protect
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (j == 1 && i == 5)
throw;
}
}
printf("continue..\n"); // not printed
}
cleanup
{
printf("cleanup\n"); // printed
}
}
catch
{
printf("catch\n");
}

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal

Re: try statement

<b08492b2-4c1b-ffdb-17ab-add45545e415@gmail.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: thiago.a...@gmail.com (Thiago Adams)
Newsgroups: comp.lang.c
Subject: Re: try statement
Date: Fri, 30 Apr 2021 09:40:07 -0300
Organization: A noiseless patient Spider
Lines: 200
Message-ID: <b08492b2-4c1b-ffdb-17ab-add45545e415@gmail.com>
References: <f5a816bb-9786-4c68-93ae-188bf8a58dd7n@googlegroups.com>
<s5i8kg$fjv$1@gioia.aioe.org>
<bfa279e9-800d-4e66-8bd6-8faf1ea68f57n@googlegroups.com>
<543fe06a-9746-416c-a7b9-fe354dbb09bdn@googlegroups.com>
<50e63b3d-5937-479b-bcf6-529f514a4becn@googlegroups.com>
<e2bcf7f9-75b8-4775-985b-005f6f3d106fn@googlegroups.com>
<3f43d338-65ba-4ef4-a584-9409f173164en@googlegroups.com>
<20210429224042.891@kylheku.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: reader02.eternal-september.org; posting-host="0150924c5ff01850b894662f410222ab";
logging-data="13478"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19cMKKyhjO2Foz6g395AVQdn1eGpQCM0O0="
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101
Firefox/60.0 SeaMonkey/2.53.6
Cancel-Lock: sha1:RCMPXCCyLZuy1LbSWARpA0wlYyM=
In-Reply-To: <20210429224042.891@kylheku.com>
 by: Thiago Adams - Fri, 30 Apr 2021 12:40 UTC

Kaz Kylheku wrote:
> On 2021-04-30, Thiago Adams <thiago.adams@gmail.com> wrote:
>> On Tuesday, April 27, 2021 at 5:45:01 PM UTC-3, Thiago Adams wrote:
>>> On Thursday, April 22, 2021 at 6:16:55 PM UTC-3, Thiago Adams wrote:
>>>> I included try-blocks and throw with a local jump.
>>> Some samples:
>> ...
>>> try {
>>> /*this is just a goto to inside catch block
>>> throw; ​
>>> }
>>> catch {
>>> /*inside catch block*/
>>> }
>>
>> Emulation of this particular case with macros.
>>
>>
>> #define try int _try_block_error = 0; do
>> #define catch while(0); catch_label:; if (_try_block_error)
>> #define throw do { _try_block_error = 1; goto catch_label; } while (0)
>>
>>
>> int main() {
>>
>> try
>> {
>> for (int i = 0; i < 10; i++)
>> {
>> for (int j = 0; j < 10; j++)
>> {
>> if (j == 1 && i == 5)
>> throw;
>> }
>> }
>> printf("continue..\n");
>> }
>> catch {
>> printf("catch\n");
>> }
>>
>> }
>
> Without the following support, through any number of nestings, it's
> pretty silly:
>
> try
> {
> unwind_protect
> {
> for (int i = 0; i < 10; i++)
> {
> for (int j = 0; j < 10; j++)
> {
> if (j == 1 && i == 5)
> throw;
> }
> }
> printf("continue..\n"); // not printed
> }
> cleanup
> {
> printf("cleanup\n"); // printed
> }
> }
> catch
> {
> printf("catch\n");
> }
>
>

I started to use these macros in a real project doing the changes
necessary and I pretty happy!

When I was doing the changes I realize in most cases I would prefer
to use a "if" instead of just automatic throw.

For instance, I propose this syntax

try {
try (FILE* f = fopen("input.txt", "r"); f; fclose(f)) throw errno;
} catch(int error) {}

But for real code it was better a "if" to create better error messages
with the context of the caller.

(
If you throw something deep inside using C++ exceptions for instance,
the changes the error message is inappropriate are big.
)

/*stores code and message*/

struct error error = {0};

FILE* f = 0; /*outsize*/

try {
f = fopen("config.txt", "r");
if (f == NULL){
seterror(&error, "cannot open config file");
throw;
}

}
catch {
printf("%s %d\n", error->message, error->code);
}

/*check and clear*/
if (f != NULL) {
fclose(f);
}

With this pattern the resources are declared outside try-block,
initialized inside and cleared outsize using a test (generally != NULL)

I don't like "returns" in the middle of my C code, but I am pretty
comfortable with this throw pattern.

Another sample.

#include <stdio.h>

#define try if (1)
#define catch else catch_label:
#define throw do { goto catch_label; } while (0)

struct Person {
char* name;
char* address;
};

void DeletePerson(struct Person* p) {
if (p)
{
free(p->name);
free(p->address);
free(p);
}
}

struct Person* CreatePerson(char* name, char * address)
{ struct Person* p = 0;
try
{
p = calloc(1, sizeof * p);
if (p == NULL) throw;
p->name = _strdup(name);
p->address = _strdup(address);
if (p->name == NULL || p->address == NULL)
{
throw;
}
}
catch
{
DeletePerson(p);
p = 0;
}
return p;
}

int main() {

struct Person* person = 0;
try
{
person = CreatePerson("name", "address");
if (person == NULL)
{
printf("out of mem\n");
throw;
}
printf("%s %s\n", person->name, person->address);
}
catch
{
}
DeletePerson(person);
}

Compare other alternatives for "CreatePerson" and you will
see that this alternative is clear.

By the way, something that simplifies code is to create a
malloc that is "alloc or die" and just ignore the result,
but I am don't fell comfortable in exit a server for instance.

Re: try statement

<87sg37epjl.fsf@bsb.me.uk>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: try statement
Date: Fri, 30 Apr 2021 16:54:22 +0100
Organization: A noiseless patient Spider
Lines: 140
Message-ID: <87sg37epjl.fsf@bsb.me.uk>
References: <f5a816bb-9786-4c68-93ae-188bf8a58dd7n@googlegroups.com>
<s5i8kg$fjv$1@gioia.aioe.org>
<bfa279e9-800d-4e66-8bd6-8faf1ea68f57n@googlegroups.com>
<543fe06a-9746-416c-a7b9-fe354dbb09bdn@googlegroups.com>
<50e63b3d-5937-479b-bcf6-529f514a4becn@googlegroups.com>
<e2bcf7f9-75b8-4775-985b-005f6f3d106fn@googlegroups.com>
<3f43d338-65ba-4ef4-a584-9409f173164en@googlegroups.com>
<20210429224042.891@kylheku.com>
<b08492b2-4c1b-ffdb-17ab-add45545e415@gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: reader02.eternal-september.org; posting-host="326f4ffde33751173afefff1ea69f127";
logging-data="15614"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19zdmIBZWxJqv7hSP8v9mMtmld68DCySxY="
Cancel-Lock: sha1:M3Ikq9UOePk+rxSDkqqF3JuFXoU=
sha1:9lJPQ5Q8UdzawtYptrFRrP2wSis=
X-BSB-Auth: 1.6c47fcca0bd5cb6f61d1.20210430165422BST.87sg37epjl.fsf@bsb.me.uk
 by: Ben Bacarisse - Fri, 30 Apr 2021 15:54 UTC

Thiago Adams <thiago.adams@gmail.com> writes:

> Kaz Kylheku wrote:
>> On 2021-04-30, Thiago Adams <thiago.adams@gmail.com> wrote:
>>> On Tuesday, April 27, 2021 at 5:45:01 PM UTC-3, Thiago Adams wrote:
>>>> On Thursday, April 22, 2021 at 6:16:55 PM UTC-3, Thiago Adams wrote:
>>>>> I included try-blocks and throw with a local jump.
>>>> Some samples:
>>> ...
>>>> try {
>>>> /*this is just a goto to inside catch block
>>>> throw; ​
>>>> }
>>>> catch {
>>>> /*inside catch block*/
>>>> }
>>>
>>> Emulation of this particular case with macros.
>>>
>>>
>>> #define try int _try_block_error = 0; do
>>> #define catch while(0); catch_label:; if (_try_block_error)
>>> #define throw do { _try_block_error = 1; goto catch_label; } while (0)
>>>
>>>
>>> int main() {
>>> try
>>> {
>>> for (int i = 0; i < 10; i++)
>>> {
>>> for (int j = 0; j < 10; j++)
>>> {
>>> if (j == 1 && i == 5)
>>> throw;
>>> }
>>> }
>>> printf("continue..\n");
>>> }
>>> catch {
>>> printf("catch\n");
>>> }
>>>
>>> }
>>
>> Without the following support, through any number of nestings, it's
>> pretty silly:
>>
>> try
>> {
>> unwind_protect
>> {
>> for (int i = 0; i < 10; i++)
>> {
>> for (int j = 0; j < 10; j++)
>> {
>> if (j == 1 && i == 5)
>> throw;
>> }
>> }
>> printf("continue..\n"); // not printed
>> }
>> cleanup
>> {
>> printf("cleanup\n"); // printed
>> }
>> }
>> catch
>> {
>> printf("catch\n");
>> }

This seems like an important point.

> I started to use these macros in a real project doing the changes
> necessary and I pretty happy!

How is it better than just leaving the label and the goto obvious for
all to see?

> Another sample.
>
> #include <stdio.h>
>
> #define try if (1)
> #define catch else catch_label:
> #define throw do { goto catch_label; } while (0)
>
> struct Person {
> char* name;
> char* address;
> };
>
> void DeletePerson(struct Person* p) {
> if (p)
> {
> free(p->name);
> free(p->address);
> free(p);
> }
> }
>
> struct Person* CreatePerson(char* name, char * address)
> {
> struct Person* p = 0;
> try
> {
> p = calloc(1, sizeof * p);
> if (p == NULL) throw;
> p->name = _strdup(name);
> p->address = _strdup(address);
> if (p->name == NULL || p->address == NULL)
> {
> throw;
> }
> }
> catch
> {
> DeletePerson(p);
> p = 0;
> }
> return p;
> }

struct Person *CreatePerson(char *name, char *address)
{
struct Person *p = calloc(1, sizeof *p);
if (p && (p->name = strdup(name)) && (p->address = strdup(address)))
return p;
DeletePerson(p);
return 0;
}

> Compare other alternatives for "CreatePerson" and you will
> see that this alternative is clear.

I can't check yours without looking at a bunch of macros. I thought
we'd left all that behind in the 1980s.

--
Ben.

Re: try statement

<1c98b264-8c18-49ac-ae09-eeea5b28fe51n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ac8:59d6:: with SMTP id f22mr5728256qtf.374.1619806692671;
Fri, 30 Apr 2021 11:18:12 -0700 (PDT)
X-Received: by 2002:a05:6214:21a6:: with SMTP id t6mr3486291qvc.23.1619806692354;
Fri, 30 Apr 2021 11:18:12 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Fri, 30 Apr 2021 11:18:12 -0700 (PDT)
In-Reply-To: <87sg37epjl.fsf@bsb.me.uk>
Injection-Info: google-groups.googlegroups.com; posting-host=189.6.254.153; posting-account=xFcAQAoAAAAoWlfpQ6Hz2n-MU9fthxbY
NNTP-Posting-Host: 189.6.254.153
References: <f5a816bb-9786-4c68-93ae-188bf8a58dd7n@googlegroups.com>
<s5i8kg$fjv$1@gioia.aioe.org> <bfa279e9-800d-4e66-8bd6-8faf1ea68f57n@googlegroups.com>
<543fe06a-9746-416c-a7b9-fe354dbb09bdn@googlegroups.com> <50e63b3d-5937-479b-bcf6-529f514a4becn@googlegroups.com>
<e2bcf7f9-75b8-4775-985b-005f6f3d106fn@googlegroups.com> <3f43d338-65ba-4ef4-a584-9409f173164en@googlegroups.com>
<20210429224042.891@kylheku.com> <b08492b2-4c1b-ffdb-17ab-add45545e415@gmail.com>
<87sg37epjl.fsf@bsb.me.uk>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <1c98b264-8c18-49ac-ae09-eeea5b28fe51n@googlegroups.com>
Subject: Re: try statement
From: thiago.a...@gmail.com (Thiago Adams)
Injection-Date: Fri, 30 Apr 2021 18:18:12 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
 by: Thiago Adams - Fri, 30 Apr 2021 18:18 UTC

On Friday, April 30, 2021 at 12:54:33 PM UTC-3, Ben Bacarisse wrote:
> Thiago Adams <thiago...@gmail.com> writes:
>
> > Kaz Kylheku wrote:
> >> On 2021-04-30, Thiago Adams <thiago...@gmail.com> wrote:
> >>> On Tuesday, April 27, 2021 at 5:45:01 PM UTC-3, Thiago Adams wrote:
> >>>> On Thursday, April 22, 2021 at 6:16:55 PM UTC-3, Thiago Adams wrote:
> >>>>> I included try-blocks and throw with a local jump.
> >>>> Some samples:
> >>> ...
> >>>> try {
> >>>> /*this is just a goto to inside catch block
> >>>> throw; ​
> >>>> }
> >>>> catch {
> >>>> /*inside catch block*/
> >>>> }
> >>>
> >>> Emulation of this particular case with macros.
> >>>
> >>>
> >>> #define try int _try_block_error = 0; do
> >>> #define catch while(0); catch_label:; if (_try_block_error)
> >>> #define throw do { _try_block_error = 1; goto catch_label; } while (0)
> >>>
> >>>
> >>> int main() {
> >>> try
> >>> {
> >>> for (int i = 0; i < 10; i++)
> >>> {
> >>> for (int j = 0; j < 10; j++)
> >>> {
> >>> if (j == 1 && i == 5)
> >>> throw;
> >>> }
> >>> }
> >>> printf("continue..\n");
> >>> }
> >>> catch {
> >>> printf("catch\n");
> >>> }
> >>>
> >>> }
> >>
> >> Without the following support, through any number of nestings, it's
> >> pretty silly:
> >>
> >> try
> >> {
> >> unwind_protect
> >> {
> >> for (int i = 0; i < 10; i++)
> >> {
> >> for (int j = 0; j < 10; j++)
> >> {
> >> if (j == 1 && i == 5)
> >> throw;
> >> }
> >> }
> >> printf("continue..\n"); // not printed
> >> }
> >> cleanup
> >> {
> >> printf("cleanup\n"); // printed
> >> }
> >> }
> >> catch
> >> {
> >> printf("catch\n");
> >> }
> This seems like an important point.
> > I started to use these macros in a real project doing the changes
> > necessary and I pretty happy!
> How is it better than just leaving the label and the goto obvious for
> all to see?

goto errorlabel;

errorlabel:

> > Another sample.
> >
> > #include <stdio.h>
> >
> > #define try if (1)
> > #define catch else catch_label:
> > #define throw do { goto catch_label; } while (0)
> >
> > struct Person {
> > char* name;
> > char* address;
> > };
> >
> > void DeletePerson(struct Person* p) {
> > if (p)
> > {
> > free(p->name);
> > free(p->address);
> > free(p);
> > }
> > }
> >
> > struct Person* CreatePerson(char* name, char * address)
> > {
> > struct Person* p = 0;
> > try
> > {
> > p = calloc(1, sizeof * p);
> > if (p == NULL) throw;
> > p->name = _strdup(name);
> > p->address = _strdup(address);
> > if (p->name == NULL || p->address == NULL)
> > {
> > throw;
> > }
> > }
> > catch
> > {
> > DeletePerson(p);
> > p = 0;
> > }
> > return p;
> > }
>
> struct Person *CreatePerson(char *name, char *address)
> {
> struct Person *p = calloc(1, sizeof *p);
> if (p && (p->name = strdup(name)) && (p->address = strdup(address)))
> return p;
> DeletePerson(p);
> return 0;
> }

I liked this solution for this specific function.

But I could not apply the same idea on the code I am using these
macros. I my code I am setting an error message for each step
not completed.

For instance:

try
{ if (Step1() != 0)
{
seterror(error, "could not complete step 1 because...");
throw;
}

if (Step2() != 0)
{
seterror(error, "could not complete step 2 because...");
throw;
}
DoSomethingOnSuccess();
} catch
{ DoSomethingOnError();
}

DoSomethingAlways();

--x--
Alternative 1 -
call DoSomethingAlways(); in two places.

if (Step1() != 0)
{
seterror(error, "could not complete step 1 because...");
goto error_label;
}

if (Step2() != 0)
{
seterror(error, "could not complete step 2 because...");
goto error_label;
}
DoSomethingOnSuccess();
DoSomethingAlways();
return;

error_label:
DoSomethingOnError();
DoSomethingAlways();
return;

--x--

Alternative 2

if (Step1() != 0)
{
seterror(error, "could not complete step 1 because...");
goto error_label;
}

if (Step2() != 0)
{
seterror(error, "could not complete step 2 because...");
goto error_label;
}

DoSomethingOnSuccess();

error_label:

if (errorvariable_set)
DoSomethingOnError();

DoSomethingAlways();
return;

Any ideas for this?

Re: try statement

<87mttfec2m.fsf@bsb.me.uk>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: try statement
Date: Fri, 30 Apr 2021 21:45:21 +0100
Organization: A noiseless patient Spider
Lines: 192
Message-ID: <87mttfec2m.fsf@bsb.me.uk>
References: <f5a816bb-9786-4c68-93ae-188bf8a58dd7n@googlegroups.com>
<s5i8kg$fjv$1@gioia.aioe.org>
<bfa279e9-800d-4e66-8bd6-8faf1ea68f57n@googlegroups.com>
<543fe06a-9746-416c-a7b9-fe354dbb09bdn@googlegroups.com>
<50e63b3d-5937-479b-bcf6-529f514a4becn@googlegroups.com>
<e2bcf7f9-75b8-4775-985b-005f6f3d106fn@googlegroups.com>
<3f43d338-65ba-4ef4-a584-9409f173164en@googlegroups.com>
<20210429224042.891@kylheku.com>
<b08492b2-4c1b-ffdb-17ab-add45545e415@gmail.com>
<87sg37epjl.fsf@bsb.me.uk>
<1c98b264-8c18-49ac-ae09-eeea5b28fe51n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: reader02.eternal-september.org; posting-host="326f4ffde33751173afefff1ea69f127";
logging-data="32230"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18EwpxjnHJI2IWplIHqUr2wnQ29px7DeAQ="
Cancel-Lock: sha1:iAg0e+r6oFBFE45kPpEIXtnq52w=
sha1:QomqR4AvR1lG5UCFHmypto2HM30=
X-BSB-Auth: 1.89987fd17e4dfc6779bd.20210430214521BST.87mttfec2m.fsf@bsb.me.uk
 by: Ben Bacarisse - Fri, 30 Apr 2021 20:45 UTC

Thiago Adams <thiago.adams@gmail.com> writes:

> On Friday, April 30, 2021 at 12:54:33 PM UTC-3, Ben Bacarisse wrote:
>> Thiago Adams <thiago...@gmail.com> writes:
>>
>> > Kaz Kylheku wrote:
>> >> On 2021-04-30, Thiago Adams <thiago...@gmail.com> wrote:
>> >>> On Tuesday, April 27, 2021 at 5:45:01 PM UTC-3, Thiago Adams wrote:
>> >>>> On Thursday, April 22, 2021 at 6:16:55 PM UTC-3, Thiago Adams wrote:
>> >>>>> I included try-blocks and throw with a local jump.
>> >>>> Some samples:
>> >>> ...
>> >>>> try {
>> >>>> /*this is just a goto to inside catch block
>> >>>> throw; ​
>> >>>> }
>> >>>> catch {
>> >>>> /*inside catch block*/
>> >>>> }
>> >>>
>> >>> Emulation of this particular case with macros.
>> >>>
>> >>>
>> >>> #define try int _try_block_error = 0; do
>> >>> #define catch while(0); catch_label:; if (_try_block_error)
>> >>> #define throw do { _try_block_error = 1; goto catch_label; } while (0)
>> >>>
>> >>>
>> >>> int main() {
>> >>> try
>> >>> {
>> >>> for (int i = 0; i < 10; i++)
>> >>> {
>> >>> for (int j = 0; j < 10; j++)
>> >>> {
>> >>> if (j == 1 && i == 5)
>> >>> throw;
>> >>> }
>> >>> }
>> >>> printf("continue..\n");
>> >>> }
>> >>> catch {
>> >>> printf("catch\n");
>> >>> }
>> >>>
>> >>> }
>> >>
>> >> Without the following support, through any number of nestings, it's
>> >> pretty silly:
>> >>
>> >> try
>> >> {
>> >> unwind_protect
>> >> {
>> >> for (int i = 0; i < 10; i++)
>> >> {
>> >> for (int j = 0; j < 10; j++)
>> >> {
>> >> if (j == 1 && i == 5)
>> >> throw;
>> >> }
>> >> }
>> >> printf("continue..\n"); // not printed
>> >> }
>> >> cleanup
>> >> {
>> >> printf("cleanup\n"); // printed
>> >> }
>> >> }
>> >> catch
>> >> {
>> >> printf("catch\n");
>> >> }
>> This seems like an important point.
>> > I started to use these macros in a real project doing the changes
>> > necessary and I pretty happy!
>> How is it better than just leaving the label and the goto obvious for
>> all to see?
>
> goto errorlabel;
>
> errorlabel:

I was not very clear. I wanted to know why you think hiding the label
and the goto inside macros helps. Any hiding of what's happening has to
be worth the cost in clarity.

>> > Another sample.
>> >
>> > #include <stdio.h>
>> >
>> > #define try if (1)
>> > #define catch else catch_label:
>> > #define throw do { goto catch_label; } while (0)
>> >
>> > struct Person {
>> > char* name;
>> > char* address;
>> > };
>> >
>> > void DeletePerson(struct Person* p) {
>> > if (p)
>> > {
>> > free(p->name);
>> > free(p->address);
>> > free(p);
>> > }
>> > }
>> >
>> > struct Person* CreatePerson(char* name, char * address)
>> > {
>> > struct Person* p = 0;
>> > try
>> > {
>> > p = calloc(1, sizeof * p);
>> > if (p == NULL) throw;
>> > p->name = _strdup(name);
>> > p->address = _strdup(address);
>> > if (p->name == NULL || p->address == NULL)
>> > {
>> > throw;
>> > }
>> > }
>> > catch
>> > {
>> > DeletePerson(p);
>> > p = 0;
>> > }
>> > return p;
>> > }
>>
>> struct Person *CreatePerson(char *name, char *address)
>> {
>> struct Person *p = calloc(1, sizeof *p);
>> if (p && (p->name = strdup(name)) && (p->address = strdup(address)))
>> return p;
>> DeletePerson(p);
>> return 0;
>> }
>
> I liked this solution for this specific function.
>
> But I could not apply the same idea on the code I am using these
> macros. I my code I am setting an error message for each step
> not completed.
>
> For instance:
>
> try
> {
> if (Step1() != 0)
> {
> seterror(error, "could not complete step 1 because...");
> throw;
> }
>
> if (Step2() != 0)
> {
> seterror(error, "could not complete step 2 because...");
> throw;
> }
> DoSomethingOnSuccess();
> }
> catch
> {
> DoSomethingOnError();
> }
>
> DoSomethingAlways();

if (Step1() != 0) {
seterror(error, "could not complete step 1 because...");
DoSomethingOnError();
}
else if (Step2() != 0) {
seterror(error, "could not complete step 2 because...");
DoSomethingOnError();
}
else DoSomethingOnSuccess();

DoSomethingAlways();

I can't see how it could be clearer than this, but maybe I have
old-fashioned views about coding.

> Any ideas for this?

I suspect this is not really what your use-case looks like, but I can
only re-write code that is shown.

--
Ben.

Re: try statement

<6d191209-30f7-4969-a1ce-902fa819f634n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:1233:: with SMTP id v19mr9969672qkj.418.1619869207422;
Sat, 01 May 2021 04:40:07 -0700 (PDT)
X-Received: by 2002:a05:620a:13e7:: with SMTP id h7mr9384851qkl.199.1619869207113;
Sat, 01 May 2021 04:40:07 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sat, 1 May 2021 04:40:06 -0700 (PDT)
In-Reply-To: <87mttfec2m.fsf@bsb.me.uk>
Injection-Info: google-groups.googlegroups.com; posting-host=189.6.254.153; posting-account=xFcAQAoAAAAoWlfpQ6Hz2n-MU9fthxbY
NNTP-Posting-Host: 189.6.254.153
References: <f5a816bb-9786-4c68-93ae-188bf8a58dd7n@googlegroups.com>
<s5i8kg$fjv$1@gioia.aioe.org> <bfa279e9-800d-4e66-8bd6-8faf1ea68f57n@googlegroups.com>
<543fe06a-9746-416c-a7b9-fe354dbb09bdn@googlegroups.com> <50e63b3d-5937-479b-bcf6-529f514a4becn@googlegroups.com>
<e2bcf7f9-75b8-4775-985b-005f6f3d106fn@googlegroups.com> <3f43d338-65ba-4ef4-a584-9409f173164en@googlegroups.com>
<20210429224042.891@kylheku.com> <b08492b2-4c1b-ffdb-17ab-add45545e415@gmail.com>
<87sg37epjl.fsf@bsb.me.uk> <1c98b264-8c18-49ac-ae09-eeea5b28fe51n@googlegroups.com>
<87mttfec2m.fsf@bsb.me.uk>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <6d191209-30f7-4969-a1ce-902fa819f634n@googlegroups.com>
Subject: Re: try statement
From: thiago.a...@gmail.com (Thiago Adams)
Injection-Date: Sat, 01 May 2021 11:40:07 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
 by: Thiago Adams - Sat, 1 May 2021 11:40 UTC

On Friday, April 30, 2021 at 5:45:32 PM UTC-3, Ben Bacarisse wrote:
> Thiago Adams <thiago...@gmail.com> writes:
>
> > On Friday, April 30, 2021 at 12:54:33 PM UTC-3, Ben Bacarisse wrote:
> >> Thiago Adams <thiago...@gmail.com> writes:
> >>
> >> > Kaz Kylheku wrote:
> >> >> On 2021-04-30, Thiago Adams <thiago...@gmail.com> wrote:
> >> >>> On Tuesday, April 27, 2021 at 5:45:01 PM UTC-3, Thiago Adams wrote:
> >> >>>> On Thursday, April 22, 2021 at 6:16:55 PM UTC-3, Thiago Adams wrote:
> >> >>>>> I included try-blocks and throw with a local jump.
> >> >>>> Some samples:
> >> >>> ...
> >> >>>> try {
> >> >>>> /*this is just a goto to inside catch block
> >> >>>> throw; ​
> >> >>>> }
> >> >>>> catch {
> >> >>>> /*inside catch block*/
> >> >>>> }
> >> >>>
> >> >>> Emulation of this particular case with macros.
> >> >>>
> >> >>>
> >> >>> #define try int _try_block_error = 0; do
> >> >>> #define catch while(0); catch_label:; if (_try_block_error)
> >> >>> #define throw do { _try_block_error = 1; goto catch_label; } while (0)
> >> >>>
> >> >>>
> >> >>> int main() {
> >> >>> try
> >> >>> {
> >> >>> for (int i = 0; i < 10; i++)
> >> >>> {
> >> >>> for (int j = 0; j < 10; j++)
> >> >>> {
> >> >>> if (j == 1 && i == 5)
> >> >>> throw;
> >> >>> }
> >> >>> }
> >> >>> printf("continue..\n");
> >> >>> }
> >> >>> catch {
> >> >>> printf("catch\n");
> >> >>> }
> >> >>>
> >> >>> }
> >> >>
> >> >> Without the following support, through any number of nestings, it's
> >> >> pretty silly:
> >> >>
> >> >> try
> >> >> {
> >> >> unwind_protect
> >> >> {
> >> >> for (int i = 0; i < 10; i++)
> >> >> {
> >> >> for (int j = 0; j < 10; j++)
> >> >> {
> >> >> if (j == 1 && i == 5)
> >> >> throw;
> >> >> }
> >> >> }
> >> >> printf("continue..\n"); // not printed
> >> >> }
> >> >> cleanup
> >> >> {
> >> >> printf("cleanup\n"); // printed
> >> >> }
> >> >> }
> >> >> catch
> >> >> {
> >> >> printf("catch\n");
> >> >> }
> >> This seems like an important point.
> >> > I started to use these macros in a real project doing the changes
> >> > necessary and I pretty happy!
> >> How is it better than just leaving the label and the goto obvious for
> >> all to see?
> >
> > goto errorlabel;
> >
> > errorlabel:
> I was not very clear. I wanted to know why you think hiding the label
> and the goto inside macros helps. Any hiding of what's happening has to
> be worth the cost in clarity.

Yes, generally I don't use macros to change the meaning of the language.
But in this case I am happy with it!

> >> > Another sample.
> >> >
> >> > #include <stdio.h>
> >> >
> >> > #define try if (1)
> >> > #define catch else catch_label:
> >> > #define throw do { goto catch_label; } while (0)
> >> >
> >> > struct Person {
> >> > char* name;
> >> > char* address;
> >> > };
> >> >
> >> > void DeletePerson(struct Person* p) {
> >> > if (p)
> >> > {
> >> > free(p->name);
> >> > free(p->address);
> >> > free(p);
> >> > }
> >> > }
> >> >
> >> > struct Person* CreatePerson(char* name, char * address)
> >> > {
> >> > struct Person* p = 0;
> >> > try
> >> > {
> >> > p = calloc(1, sizeof * p);
> >> > if (p == NULL) throw;
> >> > p->name = _strdup(name);
> >> > p->address = _strdup(address);
> >> > if (p->name == NULL || p->address == NULL)
> >> > {
> >> > throw;
> >> > }
> >> > }
> >> > catch
> >> > {
> >> > DeletePerson(p);
> >> > p = 0;
> >> > }
> >> > return p;
> >> > }
> >>
> >> struct Person *CreatePerson(char *name, char *address)
> >> {
> >> struct Person *p = calloc(1, sizeof *p);
> >> if (p && (p->name = strdup(name)) && (p->address = strdup(address)))
> >> return p;
> >> DeletePerson(p);
> >> return 0;
> >> }
> >
> > I liked this solution for this specific function.
> >
> > But I could not apply the same idea on the code I am using these
> > macros. I my code I am setting an error message for each step
> > not completed.
> >
> > For instance:
> >
> > try
> > {
> > if (Step1() != 0)
> > {
> > seterror(error, "could not complete step 1 because...");
> > throw;
> > }
> >
> > if (Step2() != 0)
> > {
> > seterror(error, "could not complete step 2 because...");
> > throw;
> > }
> > DoSomethingOnSuccess();
> > }
> > catch
> > {
> > DoSomethingOnError();
> > }
> >
> > DoSomethingAlways();
>
> if (Step1() != 0) {
> seterror(error, "could not complete step 1 because...");
> DoSomethingOnError();
> }
> else if (Step2() != 0) {
> seterror(error, "could not complete step 2 because...");
> DoSomethingOnError();
> }
> else DoSomethingOnSuccess();
>
> DoSomethingAlways();
>
> I can't see how it could be clearer than this, but maybe I have
> old-fashioned views about coding.
>
> > Any ideas for this?
>
> I suspect this is not really what your use-case looks like, but I can
> only re-write code that is shown.

I also think this solution :

if (Step1() == error) { OnError(); }
else if (Step2() == error) { OnError(); }
else { OnSuccess(); }

is good because it also looks sequential steps.

But there is some differences in the code I am using
(sorry for not posting the real code but it is not public source)
because I have other variables and steps in between and this
would created nested ifs moving the success to inside adding
a sequence of }}} at the end.

For instance:

if (Step1() == error) {
OnError();
}
else {
int index = Find();
if (index = -1) {
OnError();
}
else {
if (Step2(index) == error) {
OnError();
}
else if (Step3() == error) {
OnError();
}
else {
OnSuccess();
}
}
}

--x--

Here is a version that is more similar of what I am using
and where I am happy with these macros.

void F()
{ try
{
char var1[100];
if (Step1(var1) != 0)
{
seterror("cannot complete step1 because..");
throw;
}

char var2[100];
if (Step2(var2) != 0)
{
seterror("cannot complete step2 because..");
throw;
}

int index = Step3();
if (index == -1)
{
seterror("invalid result..");
throw;
}

if (Step4(index) != 0)
{
seterror("cannot complete step4 because..");
throw;
}

DoOnSuccess();
}
catch
{
DoOnError();
}
DoAlways();
}


Click here to read the complete article
Re: try statement

<87y2cyb3km.fsf@bsb.me.uk>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: try statement
Date: Sat, 01 May 2021 21:29:29 +0100
Organization: A noiseless patient Spider
Lines: 106
Message-ID: <87y2cyb3km.fsf@bsb.me.uk>
References: <f5a816bb-9786-4c68-93ae-188bf8a58dd7n@googlegroups.com>
<s5i8kg$fjv$1@gioia.aioe.org>
<bfa279e9-800d-4e66-8bd6-8faf1ea68f57n@googlegroups.com>
<543fe06a-9746-416c-a7b9-fe354dbb09bdn@googlegroups.com>
<50e63b3d-5937-479b-bcf6-529f514a4becn@googlegroups.com>
<e2bcf7f9-75b8-4775-985b-005f6f3d106fn@googlegroups.com>
<3f43d338-65ba-4ef4-a584-9409f173164en@googlegroups.com>
<20210429224042.891@kylheku.com>
<b08492b2-4c1b-ffdb-17ab-add45545e415@gmail.com>
<87sg37epjl.fsf@bsb.me.uk>
<1c98b264-8c18-49ac-ae09-eeea5b28fe51n@googlegroups.com>
<87mttfec2m.fsf@bsb.me.uk>
<6d191209-30f7-4969-a1ce-902fa819f634n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="8698fd4ed161faff994a161d14ec1c94";
logging-data="3696"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX183WPJtT0NxM2GJuuzLcgvfcm9Jdxz/6zo="
Cancel-Lock: sha1:7Xvb8w7kKq6V5LaE/h65I/f4U9M=
sha1:8RgRMa5/Hn6KL2q/7mcmqHsDxoA=
X-BSB-Auth: 1.f90e759610db43186e31.20210501212929BST.87y2cyb3km.fsf@bsb.me.uk
 by: Ben Bacarisse - Sat, 1 May 2021 20:29 UTC

Thiago Adams <thiago.adams@gmail.com> writes:

> I also think this solution :
>
> if (Step1() == error) { OnError(); }
> else if (Step2() == error) { OnError(); }
> else { OnSuccess(); }
>
> is good because it also looks sequential steps.
>
> But there is some differences in the code I am using

This sort of thread always turns out like this.

> (sorry for not posting the real code but it is not public source)
> because I have other variables and steps in between and this
> would created nested ifs moving the success to inside adding
> a sequence of }}} at the end.

Without equivalent code that I and other can asses, your statements
can't be challenged.

> For instance:
>
> if (Step1() == error) {
> OnError();
> }
> else {
> int index = Find();
> if (index = -1) {
> OnError();
> }
> else {
> if (Step2(index) == error) {
> OnError();
> }
> else if (Step3() == error) {
> OnError();
> }
> else {
> OnSuccess();
> }
> }
> }

I would not write that, but I don't think it's worth showing an
alternative version. Whatever I write, the real code will be different
and will absolutely not be clear written in my preferred style.

> --x--
>
> Here is a version that is more similar of what I am using
> and where I am happy with these macros.

Are the people who will come along and read your code as happy with them
as you are?

> void F()
> {
> try
> {
> char var1[100];
> if (Step1(var1) != 0)
> {
> seterror("cannot complete step1 because..");
> throw;
> }
>
> char var2[100];
> if (Step2(var2) != 0)
> {
> seterror("cannot complete step2 because..");
> throw;
> }
>
> int index = Step3();
> if (index == -1)
> {
> seterror("invalid result..");
> throw;
> }
>
> if (Step4(index) != 0)
> {
> seterror("cannot complete step4 because..");
> throw;
> }
>
> DoOnSuccess();
> }
> catch
> {
> DoOnError();
> }
> DoAlways();
> }

I would re-write this exactly as I did the other example. The locality
of declaration is not worth the ensuing mess.

You may get the impression that I am a little cross. I am, but I am
cross with myself. I knew how this would end before my first post. You
are happy, and I should really have just left it at that.

--
Ben.

Re: try statement

<ade7d87e-14a1-4664-8b1a-d3a15988d3f3n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a0c:aa04:: with SMTP id d4mr14442396qvb.16.1619955787655; Sun, 02 May 2021 04:43:07 -0700 (PDT)
X-Received: by 2002:a0c:aacd:: with SMTP id g13mr14264622qvb.41.1619955787274; Sun, 02 May 2021 04:43:07 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!4.us.feeder.erje.net!2.eu.feeder.erje.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!tr3.eu1.usenetexpress.com!feeder.usenetexpress.com!tr1.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sun, 2 May 2021 04:43:07 -0700 (PDT)
In-Reply-To: <87y2cyb3km.fsf@bsb.me.uk>
Injection-Info: google-groups.googlegroups.com; posting-host=189.6.254.153; posting-account=xFcAQAoAAAAoWlfpQ6Hz2n-MU9fthxbY
NNTP-Posting-Host: 189.6.254.153
References: <f5a816bb-9786-4c68-93ae-188bf8a58dd7n@googlegroups.com> <s5i8kg$fjv$1@gioia.aioe.org> <bfa279e9-800d-4e66-8bd6-8faf1ea68f57n@googlegroups.com> <543fe06a-9746-416c-a7b9-fe354dbb09bdn@googlegroups.com> <50e63b3d-5937-479b-bcf6-529f514a4becn@googlegroups.com> <e2bcf7f9-75b8-4775-985b-005f6f3d106fn@googlegroups.com> <3f43d338-65ba-4ef4-a584-9409f173164en@googlegroups.com> <20210429224042.891@kylheku.com> <b08492b2-4c1b-ffdb-17ab-add45545e415@gmail.com> <87sg37epjl.fsf@bsb.me.uk> <1c98b264-8c18-49ac-ae09-eeea5b28fe51n@googlegroups.com> <87mttfec2m.fsf@bsb.me.uk> <6d191209-30f7-4969-a1ce-902fa819f634n@googlegroups.com> <87y2cyb3km.fsf@bsb.me.uk>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <ade7d87e-14a1-4664-8b1a-d3a15988d3f3n@googlegroups.com>
Subject: Re: try statement
From: thiago.a...@gmail.com (Thiago Adams)
Injection-Date: Sun, 02 May 2021 11:43:07 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 140
 by: Thiago Adams - Sun, 2 May 2021 11:43 UTC

On Saturday, May 1, 2021 at 5:29:42 PM UTC-3, Ben Bacarisse wrote:
> Thiago Adams <thiago...@gmail.com> writes:
>
> > I also think this solution :
> >
> > if (Step1() == error) { OnError(); }
> > else if (Step2() == error) { OnError(); }
> > else { OnSuccess(); }
> >
> > is good because it also looks sequential steps.
> >
> > But there is some differences in the code I am using
> This sort of thread always turns out like this.
> > (sorry for not posting the real code but it is not public source)
> > because I have other variables and steps in between and this
> > would created nested ifs moving the success to inside adding
> > a sequence of }}} at the end.
> Without equivalent code that I and other can asses, your statements
> can't be challenged.
> > For instance:
> >
> > if (Step1() == error) {
> > OnError();
> > }
> > else {
> > int index = Find();
> > if (index = -1) {
> > OnError();
> > }
> > else {
> > if (Step2(index) == error) {
> > OnError();
> > }
> > else if (Step3() == error) {
> > OnError();
> > }
> > else {
> > OnSuccess();
> > }
> > }
> > }
> I would not write that, but I don't think it's worth showing an
> alternative version. Whatever I write, the real code will be different
> and will absolutely not be clear written in my preferred style.
> > --x--
> >
> > Here is a version that is more similar of what I am using
> > and where I am happy with these macros.
> Are the people who will come along and read your code as happy with them
> as you are?
> > void F()
> > {
> > try
> > {
> > char var1[100];
> > if (Step1(var1) != 0)
> > {
> > seterror("cannot complete step1 because..");
> > throw;
> > }
> >
> > char var2[100];
> > if (Step2(var2) != 0)
> > {
> > seterror("cannot complete step2 because..");
> > throw;
> > }
> >
> > int index = Step3();
> > if (index == -1)
> > {
> > seterror("invalid result..");
> > throw;
> > }
> >
> > if (Step4(index) != 0)
> > {
> > seterror("cannot complete step4 because..");
> > throw;
> > }
> >
> > DoOnSuccess();
> > }
> > catch
> > {
> > DoOnError();
> > }
> > DoAlways();
> > }
> I would re-write this exactly as I did the other example. The locality
> of declaration is not worth the ensuing mess.

I think the point is the insertion of other declarations and/or statements
between two steps that can fail and needed to be checked.
declarations can be moved.. but statements needs a logical order.

Step1()
/*declarations*/
/*other statements*/
Step2()

Without statements between..

if (Step1() == 0 &&
Step2() == 0) { /*success*/}
or

if (Step1() != 0) { /*error*/ }
else if(Step2() != 0) { /*error*/ }
else { /*success*/ }

Adding something between..

if (Step1() != 0) { goto error_label; }

/*some other statements, if for etc..*/

if(Step2() != 0) { goto error_label; }

/*success*/
return;

error_label:

return;

These try-catch-throw macros or normal goto jumps helps in the
situation where we have other statements in between avoiding nested ifs.

Compared with normal goto I think the macros helps with this last part where
it is confusing two returns.
> You may get the impression that I am a little cross. I am, but I am
> cross with myself. I knew how this would end before my first post. You
> are happy, and I should really have just left it at that.

What is interesting in C, we can have different styles and sometimes
we may be blind for using one style for many years.

Re: try statement

<6053b7a3-ca7b-4dcb-a71e-62047f0ae560n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:622a:207:: with SMTP id b7mr12913388qtx.254.1619958747684; Sun, 02 May 2021 05:32:27 -0700 (PDT)
X-Received: by 2002:a0c:e412:: with SMTP id o18mr14988521qvl.33.1619958747465; Sun, 02 May 2021 05:32:27 -0700 (PDT)
Path: i2pn2.org!i2pn.org!paganini.bofh.team!news.dns-netz.com!news.freedyn.net!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!tr2.eu1.usenetexpress.com!feeder.usenetexpress.com!tr2.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sun, 2 May 2021 05:32:27 -0700 (PDT)
In-Reply-To: <ade7d87e-14a1-4664-8b1a-d3a15988d3f3n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=189.6.254.153; posting-account=xFcAQAoAAAAoWlfpQ6Hz2n-MU9fthxbY
NNTP-Posting-Host: 189.6.254.153
References: <f5a816bb-9786-4c68-93ae-188bf8a58dd7n@googlegroups.com> <s5i8kg$fjv$1@gioia.aioe.org> <bfa279e9-800d-4e66-8bd6-8faf1ea68f57n@googlegroups.com> <543fe06a-9746-416c-a7b9-fe354dbb09bdn@googlegroups.com> <50e63b3d-5937-479b-bcf6-529f514a4becn@googlegroups.com> <e2bcf7f9-75b8-4775-985b-005f6f3d106fn@googlegroups.com> <3f43d338-65ba-4ef4-a584-9409f173164en@googlegroups.com> <20210429224042.891@kylheku.com> <b08492b2-4c1b-ffdb-17ab-add45545e415@gmail.com> <87sg37epjl.fsf@bsb.me.uk> <1c98b264-8c18-49ac-ae09-eeea5b28fe51n@googlegroups.com> <87mttfec2m.fsf@bsb.me.uk> <6d191209-30f7-4969-a1ce-902fa819f634n@googlegroups.com> <87y2cyb3km.fsf@bsb.me.uk> <ade7d87e-14a1-4664-8b1a-d3a15988d3f3n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <6053b7a3-ca7b-4dcb-a71e-62047f0ae560n@googlegroups.com>
Subject: Re: try statement
From: thiago.a...@gmail.com (Thiago Adams)
Injection-Date: Sun, 02 May 2021 12:32:27 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 121
 by: Thiago Adams - Sun, 2 May 2021 12:32 UTC

On Sunday, May 2, 2021 at 8:43:14 AM UTC-3, Thiago Adams wrote:
....
> These try-catch-throw macros or normal goto jumps helps in the
> situation where we have other statements in between avoiding nested ifs.
>
> Compared with normal goto I think the macros helps with this last part where
> it is confusing two returns.

Just to show, how some people uses jumps for error handling in C, and maybe
use this as argument to include these try-blocks in C , here is a real code
from a open source library called opc ua ansi stack.

The usage pattern in this lib is this:

{
/*jumps to BeginErrorHandling in case of error*/
OpcUa_GotoErrorIfBad(code);

/*normal function return*/
OpcUa_ReturnStatusCode; /*return code;*/

/*error return*/
OpcUa_BeginErrorHandling; /*defines Error:*/
/*error handling*/
OpcUa_FinishErrorHandling; /*return code;*/
}

This pattern can be easy adapted to use these try catch blocks
and in my opinion it brings a lot of clarity keeping the same
design that motivate the usage of these macros.

--x--
In case someone wants to see a real code from this lib.

OpcUa_StatusCode OpcUa_P_OpenSSL_HMAC_SHA1_Verify(
OpcUa_CryptoProvider* a_pProvider,
OpcUa_Byte* a_pData,
OpcUa_UInt32 a_dataLen,
OpcUa_Key* a_key,
OpcUa_ByteString* a_pSignature)
{ OpcUa_ByteString mac = OPCUA_BYTESTRING_STATICINITIALIZER;

OpcUa_InitializeStatus(OpcUa_Module_P_OpenSSL, "HMAC_SHA1_Verify");

OpcUa_ReturnErrorIfArgumentNull(a_pSignature);
OpcUa_ReturnErrorIfArgumentNull(a_key);
OpcUa_ReturnErrorIfArgumentNull(a_key->Key.Data);

if(a_key->Key.Length < 1)
{
uStatus = OpcUa_BadInvalidArgument;
OpcUa_GotoErrorIfBad(uStatus);
}

if(a_pSignature->Length != 20)
{
uStatus = OpcUa_BadInvalidArgument;
OpcUa_GotoErrorIfBad(uStatus);
}

if((OpcUa_Int32)a_dataLen < 1)
{
uStatus = OpcUa_BadInvalidArgument;
OpcUa_GotoErrorIfBad(uStatus);
}

uStatus = OpcUa_P_OpenSSL_HMAC_SHA1_Generate(
a_pProvider,
a_pData,
a_dataLen,
a_key,
&mac);
OpcUa_GotoErrorIfBad(uStatus);

if(mac.Length > 0)
{
mac.Data = (OpcUa_Byte*)OpcUa_P_Memory_Alloc(mac.Length*sizeof(OpcUa_Byte));
OpcUa_GotoErrorIfAllocFailed(mac.Data);
}
else
{
uStatus = OpcUa_Bad;
OpcUa_GotoErrorIfBad(uStatus);
}

uStatus = OpcUa_P_OpenSSL_HMAC_SHA1_Generate(
a_pProvider,
a_pData,
a_dataLen,
a_key,
&mac);
OpcUa_GotoErrorIfBad(uStatus);

if((OpcUa_MemCmp(mac.Data, a_pSignature->Data, mac.Length))==0)
{
uStatus = OpcUa_Good;
}
else
{
uStatus = OpcUa_BadSignatureInvalid;
}

if(mac.Data != OpcUa_Null)
{
OpcUa_P_Memory_Free(mac.Data);
}

OpcUa_ReturnStatusCode;
OpcUa_BeginErrorHandling;

if(mac.Data != OpcUa_Null)
{
OpcUa_P_Memory_Free(mac.Data);
}

OpcUa_FinishErrorHandling;
}

Re: try statement

<87v9818dsq.fsf@bsb.me.uk>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: try statement
Date: Sun, 02 May 2021 14:29:09 +0100
Organization: A noiseless patient Spider
Lines: 70
Message-ID: <87v9818dsq.fsf@bsb.me.uk>
References: <f5a816bb-9786-4c68-93ae-188bf8a58dd7n@googlegroups.com>
<s5i8kg$fjv$1@gioia.aioe.org>
<bfa279e9-800d-4e66-8bd6-8faf1ea68f57n@googlegroups.com>
<543fe06a-9746-416c-a7b9-fe354dbb09bdn@googlegroups.com>
<50e63b3d-5937-479b-bcf6-529f514a4becn@googlegroups.com>
<e2bcf7f9-75b8-4775-985b-005f6f3d106fn@googlegroups.com>
<3f43d338-65ba-4ef4-a584-9409f173164en@googlegroups.com>
<20210429224042.891@kylheku.com>
<b08492b2-4c1b-ffdb-17ab-add45545e415@gmail.com>
<87sg37epjl.fsf@bsb.me.uk>
<1c98b264-8c18-49ac-ae09-eeea5b28fe51n@googlegroups.com>
<87mttfec2m.fsf@bsb.me.uk>
<6d191209-30f7-4969-a1ce-902fa819f634n@googlegroups.com>
<87y2cyb3km.fsf@bsb.me.uk>
<ade7d87e-14a1-4664-8b1a-d3a15988d3f3n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="18283c204620f89ae868d6960a3bf973";
logging-data="30610"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/6OautcHiijTVWbnC+w8cwy7iwVLqqr/U="
Cancel-Lock: sha1:/ZcRZDeI+X9VmUsOzz2WgBgivEs=
sha1:2pTbLS7PXYcHOubnXeAzHrwnRI8=
X-BSB-Auth: 1.0833f52ca7534940b6b4.20210502142909BST.87v9818dsq.fsf@bsb.me.uk
 by: Ben Bacarisse - Sun, 2 May 2021 13:29 UTC

Thiago Adams <thiago.adams@gmail.com> writes:

> On Saturday, May 1, 2021 at 5:29:42 PM UTC-3, Ben Bacarisse wrote:

>> Are the people who will come along and read your code as happy with them
>> as you are?

>> I would re-write this exactly as I did the other example. The locality
>> of declaration is not worth the ensuing mess.
>
> I think the point is the insertion of other declarations and/or statements
> between two steps that can fail and needed to be checked.

So even that was not the code that needed the macros. I thought it
wouldn't be.

> declarations can be moved.. but statements needs a logical order.

> Step1()
> /*declarations*/
> /*other statements*/
> Step2()
>
> Without statements between..
>
> if (Step1() == 0 &&
> Step2() == 0) { /*success*/}
> or
>
> if (Step1() != 0) { /*error*/ }
> else if(Step2() != 0) { /*error*/ }
> else { /*success*/ }
>
> Adding something between..
>
> if (Step1() != 0) { goto error_label; }
>
> /*some other statements, if for etc..*/
>
> if(Step2() != 0) { goto error_label; }
>
> /*success*/
> return;
>
> error_label:
>
> return;

But this is not the "killer" code pattern either, is it? Should I spend
a moment showing how I would write it? No, there will just be another
example that absolutely really is totally better written using the
mystery macros.

> These try-catch-throw macros or normal goto jumps helps in the
> situation where we have other statements in between avoiding nested
> ifs.

As I said:

>> Without equivalent code that I and other can asses, your statements
>> can't be challenged.

> Compared with normal goto I think the macros helps with this last part where
> it is confusing two returns.

>> Without equivalent code that I and other can asses, your statements
>> can't be challenged.

--
Ben.

Re: try statement

<z1BjI.226267$c2cf.42185@fx28.ams4>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.uzoreto.com!news-out.netnews.com!news.alt.net!fdc2.netnews.com!peer02.ams1!peer.ams1.xlned.com!news.xlned.com!peer02.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!fx28.ams4.POSTED!not-for-mail
Subject: Re: try statement
Newsgroups: comp.lang.c
References: <f5a816bb-9786-4c68-93ae-188bf8a58dd7n@googlegroups.com>
<s5i8kg$fjv$1@gioia.aioe.org>
<bfa279e9-800d-4e66-8bd6-8faf1ea68f57n@googlegroups.com>
<543fe06a-9746-416c-a7b9-fe354dbb09bdn@googlegroups.com>
<50e63b3d-5937-479b-bcf6-529f514a4becn@googlegroups.com>
<e2bcf7f9-75b8-4775-985b-005f6f3d106fn@googlegroups.com>
<3f43d338-65ba-4ef4-a584-9409f173164en@googlegroups.com>
<20210429224042.891@kylheku.com>
<b08492b2-4c1b-ffdb-17ab-add45545e415@gmail.com> <87sg37epjl.fsf@bsb.me.uk>
<1c98b264-8c18-49ac-ae09-eeea5b28fe51n@googlegroups.com>
<87mttfec2m.fsf@bsb.me.uk>
<6d191209-30f7-4969-a1ce-902fa819f634n@googlegroups.com>
<87y2cyb3km.fsf@bsb.me.uk>
<ade7d87e-14a1-4664-8b1a-d3a15988d3f3n@googlegroups.com>
<87v9818dsq.fsf@bsb.me.uk>
From: bc...@freeuk.com (Bart)
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.10.0
MIME-Version: 1.0
In-Reply-To: <87v9818dsq.fsf@bsb.me.uk>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-GB
Content-Transfer-Encoding: 7bit
X-Antivirus: AVG (VPS 210502-6, 02/05/2021), Outbound message
X-Antivirus-Status: Clean
Lines: 47
Message-ID: <z1BjI.226267$c2cf.42185@fx28.ams4>
X-Complaints-To: http://netreport.virginmedia.com
NNTP-Posting-Date: Sun, 02 May 2021 17:09:51 UTC
Organization: virginmedia.com
Date: Sun, 2 May 2021 18:09:50 +0100
X-Received-Bytes: 3459
 by: Bart - Sun, 2 May 2021 17:09 UTC

On 02/05/2021 14:29, Ben Bacarisse wrote:
> Thiago Adams <thiago.adams@gmail.com> writes:
>
>> On Saturday, May 1, 2021 at 5:29:42 PM UTC-3, Ben Bacarisse wrote:
>
>>> Are the people who will come along and read your code as happy with them
>>> as you are?
>
>>> I would re-write this exactly as I did the other example. The locality
>>> of declaration is not worth the ensuing mess.
>>
>> I think the point is the insertion of other declarations and/or statements
>> between two steps that can fail and needed to be checked.
>
> So even that was not the code that needed the macros. I thought it
> wouldn't be.

I don't know what the capabilies are of the OP's proposal for C. But I'd
have thought the advantages of conventional try/catch are clear.

I rarely use exceptions and have only implemented them once (for
exceptions raised in user code). Using the same C-style syntax, they
might look like this:

try {
... any code ...
} catch (errorcode) {
... deal with error
}

The exception is thrown while executing the any-code block, but that
could be anywhere including deep inside a nested function:

if (some error detected) throw (errorcode);

So the advantage is that it can return directly from a nested function.
But also, that 'throw' statement could also be in inline code. Although
from the bits I've seen of the proposal, I think it needs a specific
errorcode (since this try-block might itself be executing unside another
try-block, or different errors could be thrown, needing catching
separately).

I don't know how practical this would be to emulate in C. (I did my
version for an interpreted language where stack-unwinding was easy to
do. However I remember having to prohibit gotos in and out of a
try-block as it would have screwed things up.)

Re: try statement

<87eeep7zbn.fsf@bsb.me.uk>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: try statement
Date: Sun, 02 May 2021 19:41:48 +0100
Organization: A noiseless patient Spider
Lines: 31
Message-ID: <87eeep7zbn.fsf@bsb.me.uk>
References: <f5a816bb-9786-4c68-93ae-188bf8a58dd7n@googlegroups.com>
<s5i8kg$fjv$1@gioia.aioe.org>
<bfa279e9-800d-4e66-8bd6-8faf1ea68f57n@googlegroups.com>
<543fe06a-9746-416c-a7b9-fe354dbb09bdn@googlegroups.com>
<50e63b3d-5937-479b-bcf6-529f514a4becn@googlegroups.com>
<e2bcf7f9-75b8-4775-985b-005f6f3d106fn@googlegroups.com>
<3f43d338-65ba-4ef4-a584-9409f173164en@googlegroups.com>
<20210429224042.891@kylheku.com>
<b08492b2-4c1b-ffdb-17ab-add45545e415@gmail.com>
<87sg37epjl.fsf@bsb.me.uk>
<1c98b264-8c18-49ac-ae09-eeea5b28fe51n@googlegroups.com>
<87mttfec2m.fsf@bsb.me.uk>
<6d191209-30f7-4969-a1ce-902fa819f634n@googlegroups.com>
<87y2cyb3km.fsf@bsb.me.uk>
<ade7d87e-14a1-4664-8b1a-d3a15988d3f3n@googlegroups.com>
<87v9818dsq.fsf@bsb.me.uk> <z1BjI.226267$c2cf.42185@fx28.ams4>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="18283c204620f89ae868d6960a3bf973";
logging-data="5939"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+FzPZ9mt3ieZwysimAat2fGKWuurjh0MI="
Cancel-Lock: sha1:/oS1AOmM0B8MSzQdR4yQpt4SDgA=
sha1:ZdaWjny+avnOR2spRAI3QX3ZXMg=
X-BSB-Auth: 1.5963dbf99b3b9b241771.20210502194148BST.87eeep7zbn.fsf@bsb.me.uk
 by: Ben Bacarisse - Sun, 2 May 2021 18:41 UTC

Bart <bc@freeuk.com> writes:

> On 02/05/2021 14:29, Ben Bacarisse wrote:
>> Thiago Adams <thiago.adams@gmail.com> writes:
>>
>>> On Saturday, May 1, 2021 at 5:29:42 PM UTC-3, Ben Bacarisse wrote:
>>
>>>> Are the people who will come along and read your code as happy with them
>>>> as you are?
>>
>>>> I would re-write this exactly as I did the other example. The locality
>>>> of declaration is not worth the ensuing mess.
>>>
>>> I think the point is the insertion of other declarations and/or statements
>>> between two steps that can fail and needed to be checked.
>>
>> So even that was not the code that needed the macros. I thought it
>> wouldn't be.
>
> I don't know what the capabilies are of the OP's proposal for C. But
> I'd have thought the advantages of conventional try/catch are clear.

I don't think it's wise, on Usenet, to assume that anything is clear.
Somethings will be clear to some people, but every topic will be a
complete mystery to many.

If you want to know what the OP is talking about, it's three macros
available up-thread.

--
Ben.

Re: try statement

<720a0b12-e94b-4d5b-a265-0a1976ccbc20n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ae9:df46:: with SMTP id t67mr20528317qkf.269.1620068136226;
Mon, 03 May 2021 11:55:36 -0700 (PDT)
X-Received: by 2002:a37:8181:: with SMTP id c123mr21130531qkd.287.1620068136001;
Mon, 03 May 2021 11:55:36 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Mon, 3 May 2021 11:55:35 -0700 (PDT)
In-Reply-To: <z1BjI.226267$c2cf.42185@fx28.ams4>
Injection-Info: google-groups.googlegroups.com; posting-host=189.6.254.153; posting-account=xFcAQAoAAAAoWlfpQ6Hz2n-MU9fthxbY
NNTP-Posting-Host: 189.6.254.153
References: <f5a816bb-9786-4c68-93ae-188bf8a58dd7n@googlegroups.com>
<s5i8kg$fjv$1@gioia.aioe.org> <bfa279e9-800d-4e66-8bd6-8faf1ea68f57n@googlegroups.com>
<543fe06a-9746-416c-a7b9-fe354dbb09bdn@googlegroups.com> <50e63b3d-5937-479b-bcf6-529f514a4becn@googlegroups.com>
<e2bcf7f9-75b8-4775-985b-005f6f3d106fn@googlegroups.com> <3f43d338-65ba-4ef4-a584-9409f173164en@googlegroups.com>
<20210429224042.891@kylheku.com> <b08492b2-4c1b-ffdb-17ab-add45545e415@gmail.com>
<87sg37epjl.fsf@bsb.me.uk> <1c98b264-8c18-49ac-ae09-eeea5b28fe51n@googlegroups.com>
<87mttfec2m.fsf@bsb.me.uk> <6d191209-30f7-4969-a1ce-902fa819f634n@googlegroups.com>
<87y2cyb3km.fsf@bsb.me.uk> <ade7d87e-14a1-4664-8b1a-d3a15988d3f3n@googlegroups.com>
<87v9818dsq.fsf@bsb.me.uk> <z1BjI.226267$c2cf.42185@fx28.ams4>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <720a0b12-e94b-4d5b-a265-0a1976ccbc20n@googlegroups.com>
Subject: Re: try statement
From: thiago.a...@gmail.com (Thiago Adams)
Injection-Date: Mon, 03 May 2021 18:55:36 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Thiago Adams - Mon, 3 May 2021 18:55 UTC

On Sunday, May 2, 2021 at 2:10:03 PM UTC-3, Bart wrote:
> On 02/05/2021 14:29, Ben Bacarisse wrote:
> > Thiago Adams <thiago...@gmail.com> writes:
> >
> >> On Saturday, May 1, 2021 at 5:29:42 PM UTC-3, Ben Bacarisse wrote:
> >
> >>> Are the people who will come along and read your code as happy with them
> >>> as you are?
> >
> >>> I would re-write this exactly as I did the other example. The locality
> >>> of declaration is not worth the ensuing mess.
> >>
> >> I think the point is the insertion of other declarations and/or statements
> >> between two steps that can fail and needed to be checked.
> >
> > So even that was not the code that needed the macros. I thought it
> > wouldn't be.
> I don't know what the capabilies are of the OP's proposal for C. But I'd
> have thought the advantages of conventional try/catch are clear.
>
> I rarely use exceptions and have only implemented them once (for
> exceptions raised in user code). Using the same C-style syntax, they
> might look like this:
>
> try {
> ... any code ...
> } catch (errorcode) {
> ... deal with error
> }
>
> The exception is thrown while executing the any-code block, but that
> could be anywhere including deep inside a nested function:
>
> if (some error detected) throw (errorcode);
>
> So the advantage is that it can return directly from a nested function.
> But also, that 'throw' statement could also be in inline code. Although
> from the bits I've seen of the proposal, I think it needs a specific
> errorcode (since this try-block might itself be executing unside another
> try-block, or different errors could be thrown, needing catching
> separately).
>
> I don't know how practical this would be to emulate in C. (I did my
> version for an interpreted language where stack-unwinding was easy to
> do. However I remember having to prohibit gotos in and out of a
> try-block as it would have screwed things up.)

The try-block I suggested is always local. throw must be inside the local
try block. No need for stack-unwinding.

I also suggested a variable declaration for the catch block, and throw
with an expression that set that variable.

/*type catch_variable*/
try {
throw expression; /*catch_variable = expression*/
} catch (type catch_variable) {
}

After suggesting this, I realize that in my code just throw without
any expression was sufficient. And I could emulate 100% with simple macros.

So basically this is my suggestion:

# define try if (1)
# define catch else catch_label:
# define throw do { goto catch_label; } while (0)

int main() {
try {
throw;
}
catch {
printf("catch\n");
}
printf("continue\n");
}

Re: try statement

<QTYjI.317096$E75e.251409@fx21.ams4>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!feeder1.feed.usenet.farm!feed.usenet.farm!newsfeed.xs4all.nl!newsfeed9.news.xs4all.nl!peer01.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!fx21.ams4.POSTED!not-for-mail
Subject: Re: try statement
Newsgroups: comp.lang.c
References: <f5a816bb-9786-4c68-93ae-188bf8a58dd7n@googlegroups.com>
<s5i8kg$fjv$1@gioia.aioe.org>
<bfa279e9-800d-4e66-8bd6-8faf1ea68f57n@googlegroups.com>
<543fe06a-9746-416c-a7b9-fe354dbb09bdn@googlegroups.com>
<50e63b3d-5937-479b-bcf6-529f514a4becn@googlegroups.com>
<e2bcf7f9-75b8-4775-985b-005f6f3d106fn@googlegroups.com>
<3f43d338-65ba-4ef4-a584-9409f173164en@googlegroups.com>
<20210429224042.891@kylheku.com>
<b08492b2-4c1b-ffdb-17ab-add45545e415@gmail.com> <87sg37epjl.fsf@bsb.me.uk>
<1c98b264-8c18-49ac-ae09-eeea5b28fe51n@googlegroups.com>
<87mttfec2m.fsf@bsb.me.uk>
<6d191209-30f7-4969-a1ce-902fa819f634n@googlegroups.com>
<87y2cyb3km.fsf@bsb.me.uk>
<ade7d87e-14a1-4664-8b1a-d3a15988d3f3n@googlegroups.com>
<87v9818dsq.fsf@bsb.me.uk> <z1BjI.226267$c2cf.42185@fx28.ams4>
<720a0b12-e94b-4d5b-a265-0a1976ccbc20n@googlegroups.com>
From: bc...@freeuk.com (Bart)
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.10.0
MIME-Version: 1.0
In-Reply-To: <720a0b12-e94b-4d5b-a265-0a1976ccbc20n@googlegroups.com>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-GB
Content-Transfer-Encoding: 7bit
X-Antivirus: AVG (VPS 210502-8, 02/05/2021), Outbound message
X-Antivirus-Status: Clean
Lines: 126
Message-ID: <QTYjI.317096$E75e.251409@fx21.ams4>
X-Complaints-To: http://netreport.virginmedia.com
NNTP-Posting-Date: Mon, 03 May 2021 20:17:52 UTC
Organization: virginmedia.com
Date: Mon, 3 May 2021 21:17:50 +0100
X-Received-Bytes: 5575
 by: Bart - Mon, 3 May 2021 20:17 UTC

On 03/05/2021 19:55, Thiago Adams wrote:
> On Sunday, May 2, 2021 at 2:10:03 PM UTC-3, Bart wrote:
>> On 02/05/2021 14:29, Ben Bacarisse wrote:
>>> Thiago Adams <thiago...@gmail.com> writes:
>>>
>>>> On Saturday, May 1, 2021 at 5:29:42 PM UTC-3, Ben Bacarisse wrote:
>>>
>>>>> Are the people who will come along and read your code as happy with them
>>>>> as you are?
>>>
>>>>> I would re-write this exactly as I did the other example. The locality
>>>>> of declaration is not worth the ensuing mess.
>>>>
>>>> I think the point is the insertion of other declarations and/or statements
>>>> between two steps that can fail and needed to be checked.
>>>
>>> So even that was not the code that needed the macros. I thought it
>>> wouldn't be.
>> I don't know what the capabilies are of the OP's proposal for C. But I'd
>> have thought the advantages of conventional try/catch are clear.
>>
>> I rarely use exceptions and have only implemented them once (for
>> exceptions raised in user code). Using the same C-style syntax, they
>> might look like this:
>>
>> try {
>> ... any code ...
>> } catch (errorcode) {
>> ... deal with error
>> }
>>
>> The exception is thrown while executing the any-code block, but that
>> could be anywhere including deep inside a nested function:
>>
>> if (some error detected) throw (errorcode);
>>
>> So the advantage is that it can return directly from a nested function.
>> But also, that 'throw' statement could also be in inline code. Although
>> from the bits I've seen of the proposal, I think it needs a specific
>> errorcode (since this try-block might itself be executing unside another
>> try-block, or different errors could be thrown, needing catching
>> separately).
>>
>> I don't know how practical this would be to emulate in C. (I did my
>> version for an interpreted language where stack-unwinding was easy to
>> do. However I remember having to prohibit gotos in and out of a
>> try-block as it would have screwed things up.)
>
>
> The try-block I suggested is always local. throw must be inside the local
> try block. No need for stack-unwinding.
>
> I also suggested a variable declaration for the catch block, and throw
> with an expression that set that variable.
>
> /*type catch_variable*/
> try {
> throw expression; /*catch_variable = expression*/
> }
> catch (type catch_variable) {
> }
>
> After suggesting this, I realize that in my code just throw without
> any expression was sufficient. And I could emulate 100% with simple macros.
>
> So basically this is my suggestion:
>
> # define try if (1)
> # define catch else catch_label:
> # define throw do { goto catch_label; } while (0)
>
> int main() {
> try {
> throw;
> }
> catch {
> printf("catch\n");
> }
> printf("continue\n");
> }

This has some problems:

- You can't have more than one inside a function

- You can't nest them

- You can only catch one kind of error (but one than one needs multiple
'catch' per try; probably not essential here)

But I think the first of those, and perhaps the second, can be fixed by
introducing a code for each one:

# define try if (1)
# define catch(id) else catch_label##id:
# define throw(id) do { goto catch_label##id; } while (0)

int main(void) {
try {
throw(10);
}
catch(10) {
printf("catch 10\n");
}
printf("continue 20\n");

try {
throw(20);
}
catch(20) {
printf("catch 20\n");
}

printf("continue 20\n");
}

While the last might be done by redefining catch as:

# define catch(id) else if (0) catch_label##id:

Then you can have multiple catch blocks, each with a different ID.

(Note that whatever is passed as 'id' is literally pasted - maybe
there's a better of doing it. But it means you have to consistently use
either '10' or 'ten'; you can't mix them.)

Re: try statement

<992d4163-22ea-4cd3-b5bb-a20482801511n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a37:aa0b:: with SMTP id t11mr4183144qke.70.1620076197712;
Mon, 03 May 2021 14:09:57 -0700 (PDT)
X-Received: by 2002:a05:620a:918:: with SMTP id v24mr17727044qkv.371.1620076197420;
Mon, 03 May 2021 14:09:57 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Mon, 3 May 2021 14:09:57 -0700 (PDT)
In-Reply-To: <QTYjI.317096$E75e.251409@fx21.ams4>
Injection-Info: google-groups.googlegroups.com; posting-host=189.6.254.153; posting-account=xFcAQAoAAAAoWlfpQ6Hz2n-MU9fthxbY
NNTP-Posting-Host: 189.6.254.153
References: <f5a816bb-9786-4c68-93ae-188bf8a58dd7n@googlegroups.com>
<s5i8kg$fjv$1@gioia.aioe.org> <bfa279e9-800d-4e66-8bd6-8faf1ea68f57n@googlegroups.com>
<543fe06a-9746-416c-a7b9-fe354dbb09bdn@googlegroups.com> <50e63b3d-5937-479b-bcf6-529f514a4becn@googlegroups.com>
<e2bcf7f9-75b8-4775-985b-005f6f3d106fn@googlegroups.com> <3f43d338-65ba-4ef4-a584-9409f173164en@googlegroups.com>
<20210429224042.891@kylheku.com> <b08492b2-4c1b-ffdb-17ab-add45545e415@gmail.com>
<87sg37epjl.fsf@bsb.me.uk> <1c98b264-8c18-49ac-ae09-eeea5b28fe51n@googlegroups.com>
<87mttfec2m.fsf@bsb.me.uk> <6d191209-30f7-4969-a1ce-902fa819f634n@googlegroups.com>
<87y2cyb3km.fsf@bsb.me.uk> <ade7d87e-14a1-4664-8b1a-d3a15988d3f3n@googlegroups.com>
<87v9818dsq.fsf@bsb.me.uk> <z1BjI.226267$c2cf.42185@fx28.ams4>
<720a0b12-e94b-4d5b-a265-0a1976ccbc20n@googlegroups.com> <QTYjI.317096$E75e.251409@fx21.ams4>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <992d4163-22ea-4cd3-b5bb-a20482801511n@googlegroups.com>
Subject: Re: try statement
From: thiago.a...@gmail.com (Thiago Adams)
Injection-Date: Mon, 03 May 2021 21:09:57 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Thiago Adams - Mon, 3 May 2021 21:09 UTC

On Monday, May 3, 2021 at 5:18:06 PM UTC-3, Bart wrote:
> On 03/05/2021 19:55, Thiago Adams wrote:
> > On Sunday, May 2, 2021 at 2:10:03 PM UTC-3, Bart wrote:
> >> On 02/05/2021 14:29, Ben Bacarisse wrote:
> >>> Thiago Adams <thiago...@gmail.com> writes:
> >>>
> >>>> On Saturday, May 1, 2021 at 5:29:42 PM UTC-3, Ben Bacarisse wrote:
> >>>
> >>>>> Are the people who will come along and read your code as happy with them
> >>>>> as you are?
> >>>
> >>>>> I would re-write this exactly as I did the other example. The locality
> >>>>> of declaration is not worth the ensuing mess.
> >>>>
> >>>> I think the point is the insertion of other declarations and/or statements
> >>>> between two steps that can fail and needed to be checked.
> >>>
> >>> So even that was not the code that needed the macros. I thought it
> >>> wouldn't be.
> >> I don't know what the capabilies are of the OP's proposal for C. But I'd
> >> have thought the advantages of conventional try/catch are clear.
> >>
> >> I rarely use exceptions and have only implemented them once (for
> >> exceptions raised in user code). Using the same C-style syntax, they
> >> might look like this:
> >>
> >> try {
> >> ... any code ...
> >> } catch (errorcode) {
> >> ... deal with error
> >> }
> >>
> >> The exception is thrown while executing the any-code block, but that
> >> could be anywhere including deep inside a nested function:
> >>
> >> if (some error detected) throw (errorcode);
> >>
> >> So the advantage is that it can return directly from a nested function.
> >> But also, that 'throw' statement could also be in inline code. Although
> >> from the bits I've seen of the proposal, I think it needs a specific
> >> errorcode (since this try-block might itself be executing unside another
> >> try-block, or different errors could be thrown, needing catching
> >> separately).
> >>
> >> I don't know how practical this would be to emulate in C. (I did my
> >> version for an interpreted language where stack-unwinding was easy to
> >> do. However I remember having to prohibit gotos in and out of a
> >> try-block as it would have screwed things up.)
> >
> >
> > The try-block I suggested is always local. throw must be inside the local
> > try block. No need for stack-unwinding.
> >
> > I also suggested a variable declaration for the catch block, and throw
> > with an expression that set that variable.
> >
> > /*type catch_variable*/
> > try {
> > throw expression; /*catch_variable = expression*/
> > }
> > catch (type catch_variable) {
> > }
> >
> > After suggesting this, I realize that in my code just throw without
> > any expression was sufficient. And I could emulate 100% with simple macros.
> >
> > So basically this is my suggestion:
> >
> > # define try if (1)
> > # define catch else catch_label:
> > # define throw do { goto catch_label; } while (0)
> >
> > int main() {
> > try {
> > throw;
> > }
> > catch {
> > printf("catch\n");
> > }
> > printf("continue\n");
> > }
> This has some problems:
>
> - You can't have more than one inside a function
>
> - You can't nest them
>
> - You can only catch one kind of error (but one than one needs multiple
> 'catch' per try; probably not essential here)

Yes, its not 100%. But I generally will have just one per function.

> But I think the first of those, and perhaps the second, can be fixed by
> introducing a code for each one:
> # define try if (1)
> # define catch(id) else catch_label##id:
> # define throw(id) do { goto catch_label##id; } while (0)
>
> int main(void) {
> try {
> throw(10);
> }
> catch(10) {
> printf("catch 10\n");
> }
> printf("continue 20\n");
>
> try {
> throw(20);
> }
> catch(20) {
> printf("catch 20\n");
> }
>
> printf("continue 20\n");
> }
>
> While the last might be done by redefining catch as:
>
> # define catch(id) else if (0) catch_label##id:
>
> Then you can have multiple catch blocks, each with a different ID.
>
> (Note that whatever is passed as 'id' is literally pasted - maybe
> there's a better of doing it. But it means you have to consistently use
> either '10' or 'ten'; you can't mix them.)

I prefer to pay the price for the limitation rater than add one ID each time.

(In my code generator this limitation does not exist because the id is generated)

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor