Path: utzoo!attcan!uunet!wuarchive!zaphod.mps.ohio-state.edu!lavaca.uh.edu!menudo.uh.edu!sugar!ficc!peter From: peter@ficc.ferranti.com (Peter da Silva) Newsgroups: comp.lang.c Subject: Re: Error Handling Message-ID: Date: 30 Sep 90 21:58:25 GMT References: <46.27049C42@rochgte.FIDONET.ORG> Reply-To: peter@ficc.ferranti.com (Peter da Silva) Organization: Xenix Support, FICC Lines: 72 foo() { stuff1 if(!error) { stuff2 if(!error) { stuff3 if(!error) { main code return success; } cleanup stuff3 } cleanup stuff2 } cleanup stuff1 return failure; } The main advantage of this form is it guarantees cleanups get performed in the right order. It does get a bit deep, but it's just as efficient and easier to follow than the version with gotos: foo() { stuff1 if(error) goto exception1; stuff2 if(error) goto exception2; stuff3 if(error) goto exception3; main code return success; exception3: cleanup stuff3 exception2: cleanup stuff2 exception1: cleanup stuff1 return failure; } However, if you have a couple of dozen exceptions the latter can easily be seen to be more readable. Just be DAMN careful that the exception recovery ordering is maintained. Oh, you can use a case: foo() { stuff1 state=1; if(error) goto exception; stuff2 state=2; if(error) goto exception; stuff3 state=3; if(error) goto exception; main code return success; exception: switch(state) { case 3: cleanup stuff3 case 2: cleanup stuff2 case 1: cleanup stuff1 } return failure; } But this comes down to the same thing, really. -- Peter da Silva. `-_-' +1 713 274 5180. 'U` peter@ferranti.com