public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* try, finally
@ 2008-03-19 16:13 Jason Cipriani
  2008-03-19 16:14 ` Jason Cipriani
                   ` (4 more replies)
  0 siblings, 5 replies; 23+ messages in thread
From: Jason Cipriani @ 2008-03-19 16:13 UTC (permalink / raw)
  To: gcc-help

Does GCC have anything similar to the MS and Borland compiler's __try
and __finally keywords? When using GCC I often find that I have code
like this (a moderately complex, and highly contrived, example):

====

  void *data1 = NULL, *data2 = NULL, *data3 = NULL;

  try {

    if (!(data1 = malloc(1000)))
      throw Something();
    if (!(data2 = malloc(1000)))
      throw Something();
    if (!(data3 = malloc(1000)))
      throw Something();

  } catch (...) {

    // cleanup code
    free(data3);
    free(data2);
    free(data1);
    throw;

  }

  // the same cleanup code
  free(data3);
  free(data2);
  free(data1);

===

Where I am duplicating cleanup code for normal returns and exception
handling, but what I really want to do is something like this:

===

void *data1 = NULL, *data2 = NULL, *data3 = NULL;

__try {

  if (!(data1 = malloc(1000)))
    throw Something();
  if (!(data2 = malloc(1000)))
    throw Something();
  if (!(data3 = malloc(1000)))
    throw Something();

  // do stuff

} __finally {

  // cleanup code
  free(data3);
  free(data2);
  free(data1);

}

===

Thanks,
Jason

^ permalink raw reply	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2008-03-25 22:28 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-03-19 16:13 try, finally Jason Cipriani
2008-03-19 16:14 ` Jason Cipriani
2008-03-19 17:02   ` Tim Prince
2008-03-19 16:37 ` Brian Dessent
2008-03-19 17:31 ` Ted Byers
2008-03-19 17:37 ` me22
2008-03-19 19:09   ` Jason Cipriani
2008-03-19 19:09     ` me22
2008-03-20  5:50       ` Jason Cipriani
2008-03-20 13:30       ` Noel Yap
2008-03-21  2:27         ` Jason Cipriani
2008-03-19 21:04     ` Ted Byers
2008-03-20  6:28       ` Jason Cipriani
2008-03-20 12:24         ` John Love-Jensen
2008-03-21  2:11           ` Jason Cipriani
2008-03-21  2:37             ` me22
2008-03-21  2:45               ` Jason Cipriani
2008-03-21 15:24             ` Noel Yap
2008-03-21 22:50             ` Brian Dessent
2008-03-21 23:14               ` Noel Yap
2008-03-20 14:50         ` Ted Byers
2008-03-21  2:26           ` Jason Cipriani
2008-03-25 22:28 ` Ian Lance Taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).