TITLE: throw as the last line in a function (Newsgroups: comp.lang.c++.moderated, 16 Nov 98) NAGLE: nagle@netcom.com (John Nagle) > > One of those language lawyer questions. > > int foo() > { > ... > throw("msg"); > } > > "throw" cannot return, so it should not be followed by another >statement. Thus there's no "return" statement at the end of the >function. Some compilers don't like that. What's correct? > > It's one of those annoying things where, either way, some >compiler probably won't like it. CLAMAGE: clamage@Eng.Sun.COM (Steve Clamage) If every execution path through the function winds up at the throw statement, the function return type should be void, not int, since it can't return a value. :-) More likely, the throw statement is conditional, and some other path finds a return statement. However, your scenario can occur if foo is virtual and you want to indicate an error by throwing an exception if this particular version of foo is called. One alternative is to make the function pure virtual. If that is not appropriate for some reason, put a return statement after the throw. In the worst case, a compiler will warn about unreachable code. Unreachable code is not a violation of any language rule, so no compiler should reject a program merely because it contains unreachable code. OTOH, no compiler is required to do the flow analysis to determine that a statement cannot be reached, so you are more likely to get a rejection due to a "missing" return statement.