www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Can I assert in nothrow functions ?

reply ponce <spam spam.org> writes:
Question 1:

assert can throw an AssertError in debug mode.
What happens when I mark as nothrow a function with assertions ?
The compiler accept it but I have a bad feeling about it.

Question 2:

When to use nothrow?
- Where one can be sure the code won't throw?
- Where we want the compiler to check the function and callees for 
unwanted throwing?

Thanks.
Oct 01 2011
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 10/01/2011 07:07 PM, ponce wrote:
 Question 1:

 assert can throw an AssertError in debug mode.
 What happens when I mark as nothrow a function with assertions ?
 The compiler accept it but I have a bad feeling about it.
nothrow means that a function won't throw an exception. They can still throw errors. (errors are not supposed to be caught)
 Question 2:

 When to use nothrow?
 - Where one can be sure the code won't throw?
... won't throw an exception.
 - Where we want the compiler to check the function and callees for
 unwanted throwing?
... throwing an exception. nothrow is part of the function interface. You should mark your functions with nothrow when you want to explicitly guarantee to the clients of your function that it won't throw an exception.
Oct 01 2011
parent reply bearophile <bearophileHUGS lycos.com> writes:
Timon Gehr:

 nothrow is part of the function interface. You should mark your 
 functions with nothrow when you want to explicitly guarantee to the 
 clients of your function that it won't throw an exception.
nothrow also allows some better optimization of the code. Bye, bearophile
Oct 01 2011
parent ponce <spam spam.org> writes:
Thanks Timon and Bearophile for answers.

I realize nothrow is way more useful than I first thought.
Oct 02 2011