www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - What is the exact meaning of 'nothrow'?

reply "Yuxuan Shui" <yshuiv7 gmail.com> writes:
I want to know exactly what is considered to be 'throw'.

I'm able to use dynamic arrays (which can throw 'Range 
violation') and asserts in a nothrow function. Shouldn't those be 
considered 'throw'?
Jun 10 2015
next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 06/10/2015 05:06 PM, Yuxuan Shui wrote:
 I want to know exactly what is considered to be 'throw'.

 I'm able to use dynamic arrays (which can throw 'Range violation') and
 asserts in a nothrow function. Shouldn't those be considered 'throw'?
Yes, the documentation is minimal: :) http://dlang.org/function.html#nothrow-functions It says "Nothrow functions do not throw any exceptions derived from class Exception." I have more information here but the language is misleading: http://ddili.org/ders/d.en/functions_more.html#ix_functions_more.nothrow I am going to add something like the following to clarify: "Note: Remember that it is not recommended to catch Error nor its base class Throwable. What I mean by "any exception" here is "any exception that is defined under the Exception hierarchy." A nothrow function can still emit exceptions that are under the Error hierarchy, which represents irrecoverable error conditions that should preclude the program from continuing its execution." In other words, nothrow means "does not emit Exception, it can still emit Error." Ali
Jun 10 2015
next sibling parent "Yuxuan Shui" <yshuiv7 gmail.com> writes:
On Thursday, 11 June 2015 at 00:27:36 UTC, Ali Çehreli wrote:
 On 06/10/2015 05:06 PM, Yuxuan Shui wrote:
 I want to know exactly what is considered to be 'throw'.

 I'm able to use dynamic arrays (which can throw 'Range 
 violation') and
 asserts in a nothrow function. Shouldn't those be considered 
 'throw'?
Yes, the documentation is minimal: :) http://dlang.org/function.html#nothrow-functions It says "Nothrow functions do not throw any exceptions derived from class Exception." I have more information here but the language is misleading: http://ddili.org/ders/d.en/functions_more.html#ix_functions_more.nothrow I am going to add something like the following to clarify: "Note: Remember that it is not recommended to catch Error nor its base class Throwable. What I mean by "any exception" here is "any exception that is defined under the Exception hierarchy." A nothrow function can still emit exceptions that are under the Error hierarchy, which represents irrecoverable error conditions that should preclude the program from continuing its execution." In other words, nothrow means "does not emit Exception, it can still emit Error." Ali
Thanks, that clear things up.
Jun 10 2015
prev sibling parent "Mike" <none none.com> writes:
On Thursday, 11 June 2015 at 00:27:36 UTC, Ali Çehreli wrote:

 "Note: Remember that it is not recommended to catch Error nor 
 its base class Throwable. What I mean by "any exception" here 
 is "any exception that is defined under the Exception 
 hierarchy." A nothrow function can still emit exceptions that 
 are under the Error hierarchy, which represents irrecoverable 
 error conditions that should preclude the program from 
 continuing its execution."

 In other words, nothrow means "does not emit Exception, it can 
 still emit Error."
Feel free to donate those finely worded statements to the official docs. Mike
Jun 10 2015
prev sibling parent reply "ZombineDev" <valid_email he.re> writes:
On Thursday, 11 June 2015 at 00:06:24 UTC, Yuxuan Shui wrote:
 I want to know exactly what is considered to be 'throw'.

 I'm able to use dynamic arrays (which can throw 'Range 
 violation') and asserts in a nothrow function. Shouldn't those 
 be considered 'throw'?
In D there are two types of exceptions: logic errors (derived from 'Error') and environment exceptions (derived from 'Exception'). Logic errors are considered to be irrecoverable (eg. your program should be terminated because people generally don't write code to handle them - no one wraps their array accesses in try {...} catch {...}) Environment exceptions are stuff like user input and network and file access. This are problems that you generally want to handle and that's why they're considered recoverable. So 'Exception's propagate through function calls and 'Error's terminate the program immediately. A function 'f' marked as "nothrow" indicates that no exceptions shall escape from it (if a function that 'f' calls can throw, 'f' must written, so that it catches the exception), but since 'Error's terminate the program immediately the callers of 'f' should not care about them and that's why 'Error's don't brake the 'nothrow' promise of 'f'.
Jun 10 2015
parent "ZombineDev" <valid_email he.re> writes:
On Thursday, 11 June 2015 at 00:32:45 UTC, ZombineDev wrote:
 Environment exceptions are stuff like user input and network 
 and file access. This are problems that you generally want to
... These* are ...
 handle and that's why they're considered recoverable.

 So 'Exception's propagate through function calls and 'Error's 
 terminate the program immediately.

 A function 'f' marked as "nothrow" indicates that no exceptions 
 shall escape from it (if a function that 'f' calls can throw, 
 'f' must written, so that it catches the exception), but since
... catches *all possible exceptions*), ...
 'Error's terminate the program immediately the callers of 'f' 
 should not care about them and that's why 'Error's don't brake
... don't break* ...
Jun 10 2015