草庐IT

try-Catch

全部标签

c++ - 如何使 rValue 引用在 RR 获取其值的 try block 之外可用?

假设我们不想重新设计函数a_func_that_may_throw。try{T&&rr=a_func_that_may_throw();}catch(conststd::exception&e){/*Dealwiththeexceptionhere.*/}//Question:Howtoadaptthecodeabovesoastohave`rr`availablehere?抱歉没有问清楚我的问题。添加以下内容(希望)使问题更清楚。我们可以对指针这样做:T*ptr=nullptr;try{ptr=a_source_that_may_throw();}catch(conststd::ex

c++ - gcc 和 g++ 错误 : error trying to exec 'cc1plus' : execvp: No such file or directory

我在编译时遇到问题.c和.cpp使用gcc的文件和g++,对于这两种情况,我都收到了消息:g++(orgcc):errortryingtoexec'cc1plus':execvp:Nosuchfileordirectory`我已经尝试重新安装gcc和g++并确保它们的版本相同。编辑:我使用的是ubuntu16.04.1LTS,g++和gcc的版本都是5.4.020160609。以下是echo|g++-v-xc++-fsyntax-only-的输出:Usingbuilt-inspecs.COLLECT_GCC=g++Target:x86_64-linux-gnuConfiguredwit

c++ - Catch 测试框架问题:无法使用 Catch::Session()

我在编写一些测试的C++文件中遇到此错误:error:nomembernamed'Session'innamespace'Catch'testResult=Catch::Session().run(test_argc,test_argv);~~~~~~~^查看catch.hpp单个头文件,我注意到应该实现Session()成员函数的代码是灰色的,可能是因为我找不到某个地方的#ifdef。是否有任何宏可以设置为使用session类?捕获版本:1.5.3和1.5.6。引用:https://github.com/philsquared/Catch/blob/master/docs/own-m

C++11 复制省略号和异常(catch 参数)

在代码审查之后,我们在try/catchblock中遇到了复制elison的问题。阅读此页面后:cppreferenceguide特别是这一段:Whenhandlinganexception,iftheargumentofthecatchclauseisofthesametype(ignoringtop-levelcv-qualification)astheexceptionobjectthrown,thecopyisomittedandthebodyofthecatchclauseaccessestheexceptionobjectdirectly,asifcaughtbyrefer

c++ - 为什么编译器不提示 catch 子句?

此代码生成C2248:'A::B::ExceptionB':无法访问在VS2008中的'classA::B'中声明的私有(private)类。#includeclassA{classExceptionA{};classB{classExceptionB{};public:B();};public:A(int);};A::B::B(){throwExceptionB();}A::A(inti){i%2?throwExceptionA():throwA::B::ExceptionB();//C2248!!}intmain(){try{Aa(3);}catch(A::ExceptionA&)

c++ - 为什么我收到错误 : initializing argument 1 of 'Item::Item(int)' [-fpermissive] in Eclipse when I try to compile my C++ code?

我是C++的新手,在盯着它看了太久之后终于放弃了尝试编译它。编译器似乎出于某种原因拒绝了头文件中的构造函数原型(prototype)......我无法弄清楚它有什么问题。项目.h:#ifndefITEM_H_#defineITEM_H_classItem{public:Item(int);//ThislineiswhatEclipsekeepsflaggingupwiththeerrorinthetitlevirtual~Item();Item*getNextPtr();intgetValue();voidsetNextPtr(Item*);};#endif/*ITEM_H_*/在我的

c++ - 抛出的对象分配在哪里?

这个问题在这里已经有了答案:Howareexceptionsallocatedonthestackcaughtbeyondtheirscope?(6个答案)关闭9年前。例如,当我在函数中使用throw时try{//...throwMyExceptionType()//...}catch(MyExceptionType&exp){/*...*/}MyExceptionType分配在哪里?它在堆栈上吗?如果是这样,在我的catchblock中修改exp是否安全?在catch中调用一些其他函数并使用堆栈怎么样?在类似的情况下,我有:try{charmy_array[32];throwmy_a

c++ - 在构造函数 C++ 中处理 Try-catch block

我面临这样一种情况,我需要在构造函数中使用try-catchblock。特别是,构造函数尝试调用tryblock中的函数,如果失败,它将调用另一个函数来设置一些值。在这两种情况下,构造函数都应该正确地创建对象,并且在这两种情况下它都必须在结束之前调用一些其他方法。情况如下:classA{A(inti){try{setDevice(i);}catch(DeviceException&ex){setDevice(0);throwex;}otherMethod();}}但是,如果在tryblock中发生错误,则不会调用otherMethod(),因为执行在catchblock内结束,我不能像

c++ - C++ 如何处理 try catch block 中的赋值?

我使用clang分析器检查我的C++代码是否存在缺陷和错误。我有以下构造:#include#includedoublesomethingThatMayThrow()throw(std::exception){if(rand()%2){throwstd::exception();}return5.0;}intmain(){doublevalue=2.0;try{value=somethingThatMayThrow();}catch(conststd::exception&){std::cout分析器现在提示变量value的初始值从未被读取。但是,很明显,当且仅当tryblock中出现异

c++ - boost::phoenix try_catch_all 构造编译失败

我正在编写用于日期解析的boost::spirit::qi语法。#include#include#includetemplatestructdate_rfc1123_grammar:boost::spirit::qi::grammar{typedefboost::gregorian::datevalue_type;date_rfc1123_grammar():date_rfc1123_grammar::base_type(date){namespaceqi=boost::spirit::qi;namespacephx=boost::phoenix;usingqi::_pass;usin