Raymond Chen's take on so-called zero cost exceptions

H. S. Teoh hsteoh at quickfur.ath.cx
Mon Feb 28 21:39:03 UTC 2022


On Mon, Feb 28, 2022 at 12:33:17PM -0800, Walter Bright via Digitalmars-d wrote:
> "The presence of exceptions means that the code generation is subject
> to constraints that don’t show up explicitly in the code generation:
> Before performing any operation that could potentially throw an
> exception, the compiler must spill any object state back into memory
> if the object is observable from an exception handler. (Any object
> with a destructor is observable, since the exception handler may have
> to run the destructor.) Similarly, potentially-throwing operations
> limit the compiler’s ability to reorder or eliminate loads from or
> stores to observable objects because the exception removes the
> guarantee of mainline execution."
> 
> https://devblogs.microsoft.com/oldnewthing/20220228-00/?p=106296
> 
> This is what I've been saying.

How is this any worse than explicitly checking for error codes? Isn't
the optimization situation of:

	MyObj obj;
	mayThrow();
	...
	// obj.dtor called

different from:

	MyObj obj;
	if (mayError() == ERROR)
		goto END;
	...
	END:
	// obj.dtor called

?


T

-- 
Computers aren't intelligent; they only think they are.


More information about the Digitalmars-d mailing list