Thread overview
Can I assert in nothrow functions ?
Oct 01, 2011
ponce
Oct 01, 2011
Timon Gehr
Oct 01, 2011
bearophile
Oct 02, 2011
ponce
October 01, 2011
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.
October 01, 2011
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.
October 01, 2011
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
October 02, 2011
Thanks Timon and Bearophile for answers.

I realize nothrow is way more useful than I first thought.