Yet more ideas about 'nothrows' routines [was: Checked exceptions [Re: Hijacking]]

eao197 eao197 at intervale.ru
Wed Aug 8 10:04:55 PDT 2007


On Wed, 08 Aug 2007 17:05:07 +0400, eao197 <eao197 at intervale.ru> wrote:

> So in my opinion it is better to write code which doesn't depend on  
> particular kind of exception. Assumption that any part of code could  
> throws any kind of exception lead to more reliable software (Erlang is  
> an evidence).
>
> The exception safety in much more hard in C++ where there is manual  
> memory management and there isn't scope()-constructs like in D. So I  
> hope that writting exception safe code in D is much more easy, than in  
> C++ or in Java with checked exceptions.
>
> However sometimes is good to know that some method doesn't throw  
> exception at all (it could be necessary for exception safety, like  
> C++ convection that swap() methods and destructors are exception free).  
> So I suppose to introduce 'nothrows' modifier as a sign that some  
> routine is exception free:
>
> bool is_item_in_array( int[] array, int item ) nothrows { ... }
>
> Compiler could checks nothrows-routines. It is an error if  
> nothrows-routine calls any routine without 'nothrows' modifier.
>
> So it could be an crear advice to programmer: if it has some data which  
> must be protected from exception and he calls any routine without  
> 'nothrows' modifier he must do some action to protect his data.

Yet more ideas about 'nothrows' modifier (sorry for wasting your time).

Which routines need 'nothrows' specifications?

Obvious (for me) answer is: various forms of cleanup-routines. For  
example: File.close, Mutex.release, DbConnection.disconnect and so on.  
E.g. routines which will be used in destructors, scope-constructs and  
finally-blocks. So, compiler could checks all calls in those constructs  
and reports warning (or even errors) if any method without 'nothrows' is  
being called.

Even more. In some situation cleanup actions could be performed in  
catch-blocks. To ensure that no exception will be thrown during cleanup  
programmer could write:

nothrows {
   ... // some actions...
}

and compiler will check all calls in such block.

For example:

// Create new DB connection for user.
// And checks user right. If user has no right (has expiried password) then
// exception is thrown.
UserSession
open_user_section( string user_name, string user_password )
   {
      auto db_connection = establish_db_connection();
      try
       {
          check_user_rights( db_connection, user_name, user_password );
          ... // Some other actions...
       }
      catch( Exception x )
       {
         nothrows
            {
              cleanup_db_connection( db_connection );
              ... // Some other cleanup actions...
            }
         log_session_creation_error( x );
         throw;
       }

     return new UserSession( db_connection, user_name );
   }

So compiler could check that no exception is allowed in cleanup actions.  
But new exception could be thrown in 'log_session_creation_error' method.

-- 
Regards,
Yauheni Akhotnikau



More information about the Digitalmars-d mailing list