• Re: do { quit; } else { }

    From Thiago Adams@thiago.adams@gmail.com to comp.lang.c on Wed Oct 15 16:04:10 2025
    From Newsgroup: comp.lang.c

    On 10/8/2025 9:59 AM, Thiago Adams wrote:
    ...
    Alternatives:

    try {
       throw; //error
       quit;  //early success - exit without going to catch
    }
    catch {
    }




    quit also can be emulated with macros:


    #define try if (1) {
    #define catch quit_label:; } else catch_label:
    #define throw goto catch_label
    #define quit goto quit_label

    int main(){

    try
    {
    quit; /* success exit*/
    //throw; /* catch */
    }
    catch
    {
    printf("catch\n");
    }

    printf("continuation...\n");
    }


    quit is useful for early success avoiding else { } else {}

    try
    {
    if (case1)
    {
    ret = 1;
    quit;
    }

    if (case2)
    {
    if (some_error)
    throw;
    ret = 2;
    quit;
    }

    if (case3)
    {
    ret = 3;
    quit;
    }
    }
    catch
    {
    ret = -1;
    }
    return ret;

    instead of

    if (case1)
    {
    ret = 1;
    }
    else if (case2)
    {
    if (some_error)
    {
    ret = -1;
    goto exit_label;
    }
    ret = 2;
    }
    else if (case3)
    {
    ret = 3;
    }


    exit_label:
    return ret;



    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Bonita Montero@Bonita.Montero@gmail.com to comp.lang.c on Wed Oct 15 23:04:25 2025
    From Newsgroup: comp.lang.c

    No RAII ? Silly language !

    Am 04.04.2025 um 21:23 schrieb Thiago Adams:
      What do you think of this control block?

      do
      {
         FILE f = fopen("file.txt", "r");

         if (f == NULL) quit; /*goes to else part*/

         /*success here*/
         for (int i =0; i < 10; i++){
             ...
             if (error) quit;
         }

      }
      else
      {
         /*some error*/
      }





    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Thiago Adams@thiago.adams@gmail.com to comp.lang.c on Wed Oct 15 20:41:03 2025
    From Newsgroup: comp.lang.c

    Em 15/10/2025 18:04, Bonita Montero escreveu:
    No RAII ? Silly language !




    I would be happy to chat about the disadvantages of RAII.
    I think this is for another topic, and someone could complain that is
    not about C. I think it is about C, why not introduce RAII in C.


    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Richard Heathfield@rjh@cpax.org.uk to comp.lang.c on Thu Oct 16 00:43:31 2025
    From Newsgroup: comp.lang.c

    On 16/10/2025 00:41, Thiago Adams wrote:
    Em 15/10/2025 18:04, Bonita Montero escreveu:
    No RAII ? Silly language !




    I would be happy to chat about the disadvantages of RAII.
    I think this is for another topic, and someone could complain
    that is not about C. I think it is about C, why not introduce
    RAII in C.

    Don't forget line numbers. And an environment division.
    --
    Richard Heathfield
    Email: rjh at cpax dot org dot uk
    "Usenet is a strange place" - dmr 29 July 1999
    Sig line 4 vacant - apply within
    --- Synchronet 3.21a-Linux NewsLink 1.2