草庐IT

Try-catch

全部标签

c++ - 在单个函数中混合 '__try' 和 'try' - 通过 Lambda

众所周知,WindowsSEH不支持C++异常处理机制。我们不能在单个函数中使用try和__try。这将导致编译器错误:__try{try{MayThrowCPPException_OR_SEH();}catch(...){}}__except(EXCEPTION_EXECUTE_HANDLER){}它将呈现:C2713:Onlyoneformofexceptionhandlingpermittedperfunction.大多数人不喜欢的一个选项是“YeswithSEHExceptions(/EHa)”编译器选项。这将有助于C++try/catch处理这两个异常。我们需要将这样的函数放

c++ - C++中的try catch在未命中时会影响性能吗

我有一段代码,其中函数中有一个trycatch并且函数被命中。100+次。代码每次都提早返回,而没有实际命中trycatch。这会影响VisualStudio中的性能吗?我看到了性能影响。我的代码是:voidfoo(inta){if(a>value){return;}try{possibleErrorFunction();}catch{}}我把它改成:voidfoo(inta){if(a>value){return;}bar();}voidbar(){try{possibleErrorFunction();}catch{}}第二个代码似乎快了大约10秒。对此有什么合理的解释吗?

c++ - 几个线程 : catching the moment when they all finish work

我有几个线程,我需要捕获它们全部完成工作的时刻。怎么做?for(inti=1;i 最佳答案 考虑在forblock之外创建std::thread对象并调用join()而不是detach()://empty(nothreadsassociatedtothemyet)std::arraythreads1,threads2;for(inti=0;i不调用detach()意味着必须在std的析构函数之前调用join()::thread对象被调用(无论线程是否已经完成)。出于这个原因,我将std::thread对象放在了forblock之外。

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++ - 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内结束,我不能像