Thread overview
exceptions thrown by new / object constructor ?
Nov 01, 2010
Michal Minich
Nov 01, 2010
Kagamin
Nov 01, 2010
Michal Minich
Nov 01, 2010
Jesse Phillips
Nov 02, 2010
Don
Nov 02, 2010
Michal Minich
November 01, 2010
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;
}
November 01, 2010
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
November 01, 2010
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...?
November 01, 2010
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.
November 02, 2010
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.
November 02, 2010
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.