[ros-dev] Error handling

Casper Hornstrup ch at csh-consult.dk
Mon Apr 11 22:18:37 CEST 2005



> -----Original Message-----
> From: ros-dev-bounces at reactos.com [mailto:ros-dev-bounces at reactos.com] On Behalf Of Phillip Susi
> Sent: 11. april 2005 20:53
> To: ReactOS Development List
> Subject: [ros-dev] Error handling
> 
> But sadly, I don't think that is ever going to happen.  Instead, I
> propose that we use goto to eliminate the duplicate points of return,
> and wrap the goto in a nice macro.  So far I've come up with this macro:
> 
> #define NTCALL(target, function) if( Status = function ) goto target;
> 
> And you use it like this:
> 
> NTSTATUS do_something()
> {
>     NTSTATUS Status;
> 
>     NTCALL( done, NtXXX(...) );
>     NTCALL( cleanXXX, NtYYY(...) );
>     NTCALL( cleanYYY, NtZZZ(...) );
> 
>     // manipulate x, y, and z here
> 
> cleanZZZ:
>     cleanupZZZ();
> cleanYYY:
>     cleanupYYY();
> cleanXXX:
>     cleanupXXX();
> done:
>     return Status;
> }
> 
> Now that code is more compact and maintainable than the original, and
> will produce a leaner binary.  I'm still trying to improve on it a bit
> but I like this basic idea, so I'm throwing it out for comments.  Last
> night I came up with this macro and in about 10 minutes I rewrote the
> file copy routine in usetup to use memory mapping instead of allocating
> a huge buffer, reading in the source file, then writing it out to the
> destination.  The new code involves making several more system calls
> than the old code, but since the error handling has been greatly cleaned
> up, the new code is more compact and easier to maintain.

The control flow isn't obvious since NTCALL hides much of the logic. You
should rather split the function into smaller functions with sensible
names. This will, in many cases, reduce the duplicated error handling code.

Casper




More information about the Ros-dev mailing list