Thread overview | ||||||
---|---|---|---|---|---|---|
|
October 01, 2011 Can I assert in nothrow functions ? | ||||
---|---|---|---|---|
| ||||
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 Re: Can I assert in nothrow functions ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ponce | 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 Re: Can I assert in nothrow functions ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | 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 Re: Can I assert in nothrow functions ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | Thanks Timon and Bearophile for answers. I realize nothrow is way more useful than I first thought. |
Copyright © 1999-2021 by the D Language Foundation