Checked exceptions [Re: Hijacking]

eao197 eao197 at intervale.ru
Thu Aug 9 22:49:49 PDT 2007


On Fri, 10 Aug 2007 06:03:10 +0400, Christopher Wright  
<dhasenan at gmail.com> wrote:

> eao197 wrote:
>> 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:
>
> The compiler can support this. It'd have to add more metadata to  
> precompiled libraries, but that's okay. The ddocs could then  
> (optionally?) include information on what exceptions the function throws  
> directly and what other exceptions can be thrown on invocation.
>
> So I don't see any reason for the programmer to have to manually do all  
> this.

There are one important use case where the compiler can't detect which  
exceptions are thrown by a method: polymorphism and late binding. Imagine:

class EventHandler
   {
     abstract void handle_input();
     abstract void handle_output();
     abstract void handle_exception();
     ...
   }

class SelectReactor : Reactor
   {
     void
     reactor_event_loop()
       {
         while( true )
           {
              EventHandler[] input_ready_handlers =  
detect_input_ready_handlers();
              foreach( h; input_ready_handlers )
                h.handle_input();
              ...
           }
       }
   }

During compilation of SelectReactor the compiler can't determine which  
exception could be thrown by descendants of EventHandler. So it is a  
programmer's task to specify exceptions in 'throws' clause (like in  
Java/Spec#) or declare that method doesn't throw exception at all in  
'nothrows' clause (like throw() in C++).

-- 
Regards,
Yauheni Akhotnikau



More information about the Digitalmars-d mailing list