[bind10-dev] whether/when to use exceptions

Christos Zoulas christos at zoulas.com
Wed Oct 14 17:55:33 UTC 2009


On Oct 14, 10:03am, dhankins at isc.org ("David W. Hankins") wrote:
-- Subject: Re: [bind10-dev] whether/when to use exceptions

| I have one small patch (inline);

I was debating that :-)

| On Wed, Oct 14, 2009 at 08:44:09AM -0400, Christos Zoulas wrote:
| > This is why in this case it is better to use goto's:
| > =20
| >          fp =3D fopen("foo.txt", "w");
| >          if (fp =3D=3D NULL)
| > 	     goto out;
| >=20
| >          for (i=3D0; i<10; i++)
| >              if (fprintf(fp, "%d\n", i) < 0)
| > 		 goto out;
| >=20
| -           if (fclose(fp) !=3D 0)
| - 	     got out;
| +	    if (fclose(fp) !=3D 0) {
| +	      /* Additional calls (e.g. to fclose(fp)) are undefined. */
| +	      fp =3D NULL;
| +	      goto out;
| +	    }
| >=20
| >          return SUCCESS_CODE;
| > out:
| > 	 if (fp)
| > 	     (void)fclose(fp);
| > 	 remove("foo.txt");
| > 	 return ERROR_CODE;
| 
| Alternatively you can create an 'out2' goto point after the fclose,
| this is a little faster as it skips a branch, but it further
| complicates what should be a linear code path.
| 
| There are some functions in BIND9 (or BIND8 libraries?) which have
| nested 3-or-4 deep sections of code and goto's, where each section
| had its own new constructions to de-construct, and therefore needed
| its own new goto label before the previous label.  I seem to recall
| we got those wrong sometimes (cleanup goes in the wrong section).

Well, my personal experience with pretty good c++ programmers
writing c++ with exceptions has not been good, that's why where
I work they are not allowed to do this anymore. YMMV. Why don't you
write the whole thing in java if you want exceptions? Well written
java can be as fast as c++ after the warmup period.

christos



More information about the bind10-dev mailing list