www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - exceptions thrown by new / object constructor ?

reply Michal Minich <michal.minich gmail.com> writes:
What exceptions can be throw by object constructor / new expression? 
Following code fails to compile because compiler says that both functions 
foo and bar can throw.

class C {
    nothrow this () {}
}
    
nothrow void foo () {
    auto c = new C;
}

nothrow void bar () {
    auto o = new Object;
}
Nov 01 2010
next sibling parent reply Kagamin <spam here.lot> writes:
Michal Minich Wrote:

 What exceptions can be throw by object constructor / new expression? 
 Following code fails to compile because compiler says that both functions 
 foo and bar can throw.
 
 class C {
     nothrow this () {}
 }
     
 nothrow void foo () {
     auto c = new C;
 }
 
 nothrow void bar () {
     auto o = new Object;
 }
GC can throw OutOfMemoryException
Nov 01 2010
parent Michal Minich <michal.minich gmail.com> writes:
On Mon, 01 Nov 2010 15:17:11 -0400, Kagamin wrote:

 Michal Minich Wrote:
 
 What exceptions can be throw by object constructor / new expression?
 Following code fails to compile because compiler says that both
 functions foo and bar can throw.
 
 class C {
     nothrow this () {}
 }
     
 nothrow void foo () {
     auto c = new C;
 }
 
 nothrow void bar () {
     auto o = new Object;
 }
GC can throw OutOfMemoryException
http://www.digitalmars.com/d/2.0/phobos/std_gc.html in GC docs it says it throw OutOfMemoryException, but shouldn't that be OutOfMemoryError ? Isn't the docs just outdated...?
Nov 01 2010
prev sibling parent reply Jesse Phillips <jessekphillips+D gmail.com> writes:
I thought there was a Bug report on this, but I guess not. I say report it.
Ether it should compile or the compiler should error that a constructor can not
be nothrow.
Nov 01 2010
parent reply Don <nospam nospam.com> writes:
Jesse Phillips wrote:
 I thought there was a Bug report on this, but I guess not. I say report it.
Ether it should compile or the compiler should error that a constructor can not
be nothrow.
This has already been fixed in svn, and will be in the next release. See bug 3020. nothrow is painful and limited in 2.050, but should be quite usable in 2.051.
Nov 02 2010
parent Michal Minich <michal.minich gmail.com> writes:
On Tue, 02 Nov 2010 15:58:51 +0100, Don wrote:

 Jesse Phillips wrote:
 I thought there was a Bug report on this, but I guess not. I say report
 it. Ether it should compile or the compiler should error that a
 constructor can not be nothrow.
This has already been fixed in svn, and will be in the next release. See bug 3020. nothrow is painful and limited in 2.050, but should be quite usable in 2.051.
Great! Many thanks! This issue has greatly limited nothrow usage because 'new' is so common.
Nov 02 2010