www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Exception Handling, Scope and Destructor questions

reply orgoton <orgoton mindless.com> writes:
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;

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?
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?
Feb 12 2007
parent reply Kirk McDonald <kirklin.mcdonald gmail.com> writes:
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?
 

Yes.
 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 http://pyd.dsource.org
Feb 12 2007
next sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Kirk McDonald" <kirklin.mcdonald gmail.com> wrote in message 
news:eqqjbc$ebp$1 digitalmars.com...

 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.

Wow, you learn something new every day :) Never knew you could make try/catch/finally statements without the braces. Huh. I'll have to add that to MiniD.
Feb 12 2007
parent Kirk McDonald <kirklin.mcdonald gmail.com> writes:
Jarrett Billingsley wrote:
 "Kirk McDonald" <kirklin.mcdonald gmail.com> wrote in message 
 news:eqqjbc$ebp$1 digitalmars.com...
 
 Lastly, the catch() does not need to have a scope, yes? something like 
 catch(Exception e) ProcessGeneric(); in summary of the code above?


Wow, you learn something new every day :) Never knew you could make try/catch/finally statements without the braces. Huh. I'll have to add that to MiniD.

C++'s treatment of this topic is one of its many freakish features: http://www.everything2.com/index.pl?node_id=1429017 -- Kirk McDonald http://kirkmcdonald.blogspot.com Pyd: Connecting D and Python http://pyd.dsource.org
Feb 12 2007
prev sibling parent reply Deewiant <deewiant.doesnotlike.spam gmail.com> writes:
Kirk McDonald wrote:
 orgoton wrote:
 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.

Incorrect. try doesn't need it, but catch does, and I would expect finally to, as well. try foo(); catch bar(); // doesn't work try foo(); catch { bar(); } // fine
Feb 13 2007
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Deewiant" <deewiant.doesnotlike.spam gmail.com> wrote in message 
news:eqsvi1$8uv$1 digitalmars.com...
 Incorrect. try doesn't need it, but catch does, and I would expect finally 
 to,
 as well.

 try foo();
 catch bar(); // doesn't work

 try foo();
 catch { bar(); } // fine

This works: try foo(); catch(Object o) // notice you have to put an exception name here bar(); finally baz();
Feb 13 2007