__declspec(noreturn)



Hi!

I have a utility function that composes an error message and throws a
std::runtime_error with it:

void throw_error( char const* str)
{
std::string msg;
msg += "my tool:";
msg += str;
throw std::runtime_error(msg);
}

So far, so good, but when I have a function that searches and returns
something:

foo const* search_foo()
{
...
// not found
throw_error("couldn't find foo");
}

The compiler complains that not all paths return something. Of course, I
could add a return 0 statement after the throw_error call, but my idea was
to tell the compiler that throw_error doesn't return by declaring it like
this:

void __declspec(noreturn) throw_error( char const* str);

Now, the compiler also doesn't complain, just as the docs[1] say. However, I
only found use of this function with things like exit(), _exit() and
abort(), which indeed do not return by terminating the program.

Now to my question: is this technique safe when the function throws an
exception? I could well imagine that cleanup code that would have to be
executed during stack unwinding is omitted for functions that have the
noreturn attribute - it is not explicitly documented if it works for
functions that always throw.

Can anybody shed some light on this?

Uli

[1] first hit for 'noreturn' on MSDN.
--
http://got.to/quote
Read this as I won't help you unless you make your posts readable or provide
a fascinating problem.

.



Relevant Pages

  • Re: namespaces conflict?
    ... You don't modify the str argument so make it char const *. ... you're using the compiler in a non conforming mode ...
    (comp.lang.cpp)
  • Re: Why reinvent the wheel?
    ... Private Shared Function MsgToString(ByVal msg As Int32) As String ... Dim str As String ... Public Function GetMessageName(ByVal nMsg As Long) As String ...
    (microsoft.public.vb.general.discussion)
  • Re: safe alternative to va_arg
    ... error messages (I will probably make this a bit more general with some ... Passing 'int' by reference is overkill. ... print(char const* msg, A1 const& a1); ...
    (microsoft.public.vc.language)
  • Re: Macro to add date automatically to MEETING INVITE
    ... Well for one thing msg isn't assigned to anything so you're trying to add to the Body of a null object. ... Dim msg As Outlook.MeetingItem ... Dim str As String ... but for some reason not with a meeting invite. ...
    (microsoft.public.office.developer.outlook.vba)
  • Re: question on try/catch
    ... > I tried the following code about try/catch ... > using namespace std; ... catch (char const *str) ...
    (comp.lang.cpp)

Quantcast