www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Should C functions automatically be nothrow?

reply Jonathan M Davis <jmdavisProg gmx.com> writes:
Can C functions throw? I don't know of any way that a C function could throw. 
Is it possible if you have a C function which calls a D function or something 
like that? I don't know. I wouldn't really expect the C function to be able to 
handle the D exception, in which case, it wouldn't end up throwing the 
exception to whatever code calls it.

Assuming that C functions can't throw, is there any reason _not_ to have the 
compiler automatically treat C functions as nothrow?

If we can't treat C funtions in general as nothrow (for whatever reason that 
may be), is there a reason why we can't explicitly mark the various C bindings 
in druntime as nothrow at the very least?

- Jonathan M Davis
Feb 06 2012
next sibling parent Walter Bright <newshound2 digitalmars.com> writes:
On 2/6/2012 6:04 PM, Jonathan M Davis wrote:
 Can C functions throw?
Yes. There's no reason you cannot write: extern (C) void foo() { throw new Exception(); }
Feb 06 2012
prev sibling next sibling parent deadalnix <deadalnix gmail.com> writes:
Le 07/02/2012 03:04, Jonathan M Davis a écrit :
 Can C functions throw? I don't know of any way that a C function could throw.
 Is it possible if you have a C function which calls a D function or something
 like that? I don't know. I wouldn't really expect the C function to be able to
 handle the D exception, in which case, it wouldn't end up throwing the
 exception to whatever code calls it.

 Assuming that C functions can't throw, is there any reason _not_ to have the
 compiler automatically treat C functions as nothrow?

 If we can't treat C funtions in general as nothrow (for whatever reason that
 may be), is there a reason why we can't explicitly mark the various C bindings
 in druntime as nothrow at the very least?

 - Jonathan M Davis
extern(C) goes in both directions. D can call C, but C can call D. And D can throw exceptions. So you can end up with extern(C) function that throw exceptions.
Feb 07 2012
prev sibling parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
On 07/02/2012 02:04, Jonathan M Davis wrote:
 Can C functions throw? I don't know of any way that a C function could throw.
<snip> On top of what the others have said, functions written in C can certainly throw such things as AVs. Stewart.
Feb 07 2012
next sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 02/07/2012 02:40 PM, Stewart Gordon wrote:
 On 07/02/2012 02:04, Jonathan M Davis wrote:
 Can C functions throw? I don't know of any way that a C function could
 throw.
<snip> On top of what the others have said, functions written in C can certainly throw such things as AVs. Stewart.
AVs are not thrown. (if they were, they could be caught.)
Feb 07 2012
prev sibling parent reply Walter Bright <newshound2 digitalmars.com> writes:
On 2/7/2012 5:40 AM, Stewart Gordon wrote:
 On 07/02/2012 02:04, Jonathan M Davis wrote:
 Can C functions throw? I don't know of any way that a C function could throw.
<snip> On top of what the others have said, functions written in C can certainly throw such things as AVs.
Although seg faults are converted into D exceptions on Windows by druntime, and so can be caught, I suspect this was not a good idea. It's completely non-portable to other systems. Nor would C code do such a conversion anyway.
Feb 07 2012
next sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Tue, Feb 07, 2012 at 11:53:45AM -0800, Walter Bright wrote:
 On 2/7/2012 5:40 AM, Stewart Gordon wrote:
On 07/02/2012 02:04, Jonathan M Davis wrote:
Can C functions throw? I don't know of any way that a C function could throw.
<snip> On top of what the others have said, functions written in C can certainly throw such things as AVs.
Although seg faults are converted into D exceptions on Windows by druntime, and so can be caught, I suspect this was not a good idea. It's completely non-portable to other systems.
[...] I was going to suggest using siglongjmp() and doing stack-unwinding in the SEGV signal handler on Posix systems, but apparently this is very very evil because subsequent code will still be running in the signal handler's context and any call to signal-unsafe functions will cause undefined behaviour. So yeah. Bad idea. :) T -- Time flies like an arrow. Fruit flies like a banana.
Feb 07 2012
parent Johannes Pfau <nospam example.com> writes:
Am Tue, 7 Feb 2012 12:31:06 -0800
schrieb "H. S. Teoh" <hsteoh quickfur.ath.cx>:

 On Tue, Feb 07, 2012 at 11:53:45AM -0800, Walter Bright wrote:
 On 2/7/2012 5:40 AM, Stewart Gordon wrote:
On 07/02/2012 02:04, Jonathan M Davis wrote:
Can C functions throw? I don't know of any way that a C function
could throw.
<snip> On top of what the others have said, functions written in C can certainly throw such things as AVs.
Although seg faults are converted into D exceptions on Windows by druntime, and so can be caught, I suspect this was not a good idea. It's completely non-portable to other systems.
[...] I was going to suggest using siglongjmp() and doing stack-unwinding in the SEGV signal handler on Posix systems, but apparently this is very very evil because subsequent code will still be running in the signal handler's context and any call to signal-unsafe functions will cause undefined behaviour. So yeah. Bad idea. :) T
Does libsigsegv have the same restrictions or how is it implemented?
Feb 08 2012
prev sibling parent "Martin Nowak" <dawg dawgfoto.de> writes:
On Tue, 07 Feb 2012 20:53:45 +0100, Walter Bright  
<newshound2 digitalmars.com> wrote:

 On 2/7/2012 5:40 AM, Stewart Gordon wrote:
 On 07/02/2012 02:04, Jonathan M Davis wrote:
 Can C functions throw? I don't know of any way that a C function could  
 throw.
<snip> On top of what the others have said, functions written in C can certainly throw such things as AVs.
Although seg faults are converted into D exceptions on Windows by druntime, and so can be caught, I suspect this was not a good idea. It's completely non-portable to other systems. Nor would C code do such a conversion anyway.
I'm not a big fan of translating async exceptions which causes enough bad usage in C++ land. We're translating them to Errors though. Visual C++ allows to switch between different EH models http://msdn.microsoft.com/en-us/library/1deeycx5(v=vs.100).aspx.
Feb 07 2012