Exception Handling, Scope and Destructor questions

Kirk McDonald kirklin.mcdonald at gmail.com
Mon Feb 12 12:42:26 PST 2007

orgoton wrote:
> The D specification says that when an object is destroyer (either implicitly or by GC action) the destructors of al inherited classes are also called. However, if I have a class "one" and a class "two", which inherits from class "one", if I call any instance of "two", the destructor of "one" also gets called. The question is: if I use a reference to "two" in form of "one", will the destructor of "two" be called?
> ONE handle=new TWO;
> delete handle;

Yes. Destructors are virtual.

> Next question:
> I have a try block which may have one of several exceptions thrown, and depending on which, I call a different catch:
> try
> {
> something();}
> catch(ExceptionType1 e)
> {
> process();
> }
> catch(ExceptionType2 e)
> {
> process 2;
> }
> catch(Exception e)
> {
> ProcessGeneric();
> }
> Since ExceptionType1 and Type2 both inherit from call Exception, does the last catch execute along with Type1 or Type2?

No. The first matching catch block is the one that is used. The last one 
is only executed when an exception other than Type1 or Type2 is thrown.

> Please confirm, the finally{} block ALWAYS gets called, right?


> Lastly, the catch() does not need to have a scope, yes? something like catch(Exception e) ProcessGeneric(); in summary of the code above?

Yes, this is allowed.

Kirk McDonald
Pyd: Wrapping Python with D

More information about the Digitalmars-d-learn mailing list