Sunday 29 September 2013

Writing a Function that Never Returns in C#

Unfortunately, there is no standard way of declaring this e.g. "public never FunctionThatNeverReturns()" is one possible syntax but is not part of standard C#.

In fact, it's not part of standard C++ either. However, some compilers, such as GCC, have a way to express this by declaring function attributes which are enclosed within double round brackets.

void fatal() __attribute__ ((noreturn)) { ... }

Unfortunately, adding this attribute does not mean the function will not return control to the caller in exceptional cases, e.g. when an exception is thrown or a longjmp is attempted.

No comments: