开发者

Unhandled exception check in C++

Is there any way in C++ to get a compile-ti开发者_开发百科me error or warning if an exception is unhandled? For example, consider this Java code snippet:

public void f()
{
    g();  // <-- Java compiler reports "Unhandled exception" error here.
}

public void g() throws Exception
{
}

Can g++ or MSVC do something similar? Or is there any external code parsing tool which can do this? I suppose one could programmatically extend the C++ parser of Eclipse CDT to achieve this, but... are there easier solutions?


Simply, no. C++ exception specifications have a different meaning to Java's exception specifications and they are now deprecated so you should consider avoiding them if possible.

In C++, the checks requested by exception specification are enforced only at runtime. In C++, if a function violates its exception specification the the "unexpected" handler is called. It is not a compile time error to call a function that can potentially throw an arbitrary exception from a function with an exception specification.

ISO/IEC 14882:2011 15.4 [except.spec] / 11:

An implementation shall not reject an expression merely because when executed it throws or might throw an exception that the containing function does not allow.


Short answer: no. There are no checked exceptions in C++. You can try to use an external tool, but TBH I've never seen anyone do that. Just document the interfaces properly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜