本文介绍了main()周围没有大括号 - 为什么这样工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 我正在编写一本关于C ++的书,以及有关这个程序的错误一章(我留下了一些小事,但大部分是这样): int main() try { //我们的程序( return 0; } catch(exception& e){ cerr<< error:< e.what()< '\\\'; return 1; } catch(...){ cerr<< 未知异常\\\; return 2; } 这是编译但是没有什么, / p> 为什么在main()之后没有一组花括号括起来的东西? 如果他们是函数,那么在catch之前没有int(无论如何) 如果他们不是函数,它们是什么? re catch(...),我从来没有见过椭圆办法。我可以在任何地方使用省略号表示任何东西吗? 解决方案 1为什么没有一套花括号括在main()之后的所有内容? ... 有,它只有关键字 try 在 main 结束后之后有一些 catch ...块是或者我应该称它们为catchphrases(ha!)是main()的一部分吗? 2如果他们是函数,怎么会在catch(无论)之前没有int? 3如果他们不是函数, 它们是关键字,而不是函数,它们是main的一部分,尽管 try 进入 int main()定义和它的 {} 4 re catch(...)从未见过的椭圆使用那种方式。我可以在任何地方使用省略号来表示任何东西吗? 在C ++中有几个重载的省略号意义。 catch(...)表示捕捉任何 int printf(char *,...)意味着函数接受一个变量参数列表,完全禁止对参数进行类型检查,并且很容易出错(但偶尔有用) template< typename ... TypePack> 表示模板接受变量类型列表,这对元编程非常有用, li> #define DEBUG(str,...)是一个可变参数宏,类似于变量参数函数 功能级尝试 / catch 块是在异常处理程序中包装整个函数体的一种方法。所以,这里,主要的功能块在 try {...} 里面。 特别是允许构造函数用 try / catch 包装它们的初始化器列表,以处理从subobject构造函数抛出的异常。 / p> 例如。 (激励情况) C :: C()try:Base(1),member(2) { ... } catch(...){ // handle Base :: Base or member construction failed here } 请注意,没有其他方法捕获从基类或成员subobject构造函数抛出的异常,因为他们总是至少在你的构造函数体启动,即使省略了初始化程序列表。 I'm working through a book on C++ and in the chapter on errors it has this program (I left a couple of minor things out but it's mostly this):int main()try { // our program (<- this comment is literally from the book) return 0;}catch(exception& e) { cerr << "error: " << e.what() << '\n'; return 1;}catch(...) { cerr << "Unknown exception\n"; return 2;}This compiled but of course it did nothing so I'm still wondering aboutwhy there isn't a set of curly braces enclosing everything after main()? Are the blocks or shall I call them "catchphrases" (ha!) part of main() or not?If they are functions how come there's no "int" before catch(whatever)?If they're not functions, what are they?re catch(...), I've never seen ellipses used that way. Can I use ellipses anywhere to mean "anything"? 解决方案 1 why there isn't a set of curly braces enclosing everything after main()? ...There is, it just has the keyword try before the opening brace, and some catch blocks after the end of main. ... Are the blocks or shall I call them "catchphrases" (ha!) part of main() or not? 2 If they are functions how come there's no "int" before catch(whatever)? 3 If they're not functions, what are they?They're keywords, not functions, and they're part of main, despite the try coming between the int main() definition and its {} body. See the motivating case below for another example. 4 re catch(...), I've never seen ellipses used that way. Can I use ellipses anywhere to mean "anything"?there are a couple of overloaded meanings of ellipsis in C++.catch(...) means catch anything, it's like a wildcard for the exception type (and should be the last catch if you have several)int printf(char *, ...) means a function takes a variable argument list, which entirely disables type-checking of arguments and is easy to get wrong (but occasionally useful)template <typename... TypePack> means a template accepts a variable type list, which is very useful for metaprogramming but completely out of scope here#define DEBUG(str, ...) is a variadic macro, analogous to the variable-argument functionFunction-level try/catch blocks are a way of wrapping the whole function body in an exception handler. So, here, the main function block is inside the try { ... }.IIRC this was introduced specifically to allow constructors to wrap their initializer lists with a try/catch, to handle exceptions thrown from subobject constructors.Eg. (motivating case)C::C() try : Base(1), member(2){ ...}catch (...) { // handle Base::Base or member construction failure here}Note that there is no other way to catch exceptions thrown from base class or member subobject constructors, because they'll always do at least default construction before your constructor body starts, even if you omit the initializer list. 这篇关于main()周围没有大括号 - 为什么这样工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的.. 09-06 11:23