开发者

GCC comp_except_specs and exception handling/parsing

I'm a bit over my head here... I've begun digging into the GCC G++ and am trying to throw warning/errors if a function throws exceptions not defined开发者_运维问答 in an exception-specification or vice-versa (you know, like java).

I feel like I've walked into a whole new planet filled with arbitrary macros that take in tree's of all different types, spits them out and I've no clue how to interface with them...

I've located comp_except_specs which compares two exception types and returns whether they're equivalent; however, just passing the returned exception from build_throw and the tree returned from cp_parser_exception_specification_opt causes an internal compiler error.

If you've followed any of that, chances are you have more knowledge on this matter than I do. I'm not looking for the "answer" although that'd be nice but any information on this would be extremely appreciated!

Edit: My goal was to alter the compiler (not sure why I didn't mention that at the time...) to incorporated Java style exception-specification errors. I eventually did come up with a good way to implement it - thought I don't remember how, was too long ago now.


I hate to disappoint you, but but as far as I know Java-like exception checking isn't possible in C++, not even as a GCC-specific extension.

This was an oversight caused by the fact that exceptions were bolted onto the language in a very late stage, without due deliberation. Worse, something that looks similar, but does something completely different and usually counter-productive, was added in the form of exception specifications.

Given that fiasco and its implications in terms of e.g. required syntax for Java-like checking, and a general feeling of exception defeat and fatigue in the C++ community, we probably won't get proper exception checking in C++ ever.


EDIT I'm not really sure what you're actually asking, beyond stating that the source code of g++ is complicated.

Old answer below (which doesn't have that much to do with the actual question, it seems, but I'll leave it in case somebody googles it up someday and finds it useful)

As another answer says, C++ doesn't have Java-style checked exceptions [1]. In fact, the next iteration of the C++ standard, C++11 (previously C++0x) deprecated exception specifications, and instead adds a "noexcept" keyword that can be used instead of the old "throw()". See e.g.

http://www.gotw.ca/publications/mill22.htm

"noexcept" vs "Throws: nothing"

http://www.boost.org/development/requirements.html#Exception-specification

[1] Of course, there are people who use Java and don't like checked exceptions, and some frameworks (Spring IIRC?) wrap standard exceptions in unchecked ones (inheriting from RuntimeException).


Please be more specific in your question, this is too broad.

Exception specifications are pretty much useless in C++ as it is. The compiler doesn't check against it. I've heard about static analysis tools that do. However I haven't actually encountered one yet.

It's a shame really, exceptions are a superior way to handle errors compared to undefined or dubious behavior. It's just one more thing C++ got it wrong and C++0x just aggravates it.

And G++ hacking I believe, is waaaay out of scope of this site. GCC/G++ mailing lists would be more appropriate for this.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜