c – 使用多个参数捕获
发布时间:2020-12-16 10:17:50 所属栏目:百科 来源:网络整理
导读:首先我在 cplusplus.com中找到以下引用: The catch format is similar to a regular function that always has at least one parameter. 但我试过这个: try{ int kk3,k4; kk3=3; k4=2; throw (kk3,"hello");}catch (int param){ cout "int exception"param
首先我在
cplusplus.com中找到以下引用:
但我试过这个: try { int kk3,k4; kk3=3; k4=2; throw (kk3,"hello"); } catch (int param) { cout << "int exception"<<param<<endl; } catch (int param,string s) { cout<<param<<s; } catch (char param) { cout << "char exception"; } catch (...) { cout << "default exception"; } 编译器不会抱怨带有大括号和多个参数的throw.但它实际上抱怨了多个参数的捕获,尽管参考文献说的是什么.我糊涂了.尝试和捕获是否允许这种多样性?如果我想抛出一个包含多个变量的异常,或者没有相同的类型,该怎么办? 解决方法
(kk3,“hello”)是一个逗号表达式.逗号表达式计算从左到写的所有参数,结果是最右边的参数.所以在表达中
int i = (1,3,4); 我变成了4. 如果你真的想抛出它们(出于某种原因),你可以这样抛出 throw std::make_pair(kk3,std::string("hello")); 并抓住这样: catch(std::pair<int,std::string>& exc) { } 一个catch子句只有一个参数或者 ... HTH (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |