草庐IT

throw-catch

全部标签

android - 如何在不使用 try/catch 在 android 中处理它的情况下获得 401 响应

我正在使用HttpUrlConnection从我的android应用程序发出网络请求。一切正常,除了一件事,401。每当服务器返回状态代码为401的响应时,我的应用程序都会抛出IOException并显示一条消息,说明“未找到身份验证挑战”。谷歌搜索后,我没有找到单一的解决方案,只有解决方法(使用try/catch处理它,假设它有401响应)。这是代码fragment:publicBundlerequest(Stringaction,Bundleparams,Stringcookie)throwsFileNotFoundException,MalformedURLException,S

c++ - 从 catch 语句中使用 "go to"可以吗

我被告知的一切都是去那里是邪恶的并且远离他们,但我认为他们可以在这里帮助我(?)。我想为用户提供一个选项,以便在捕获到异常时重新启动应用程序,并且在思考要做什么时遇到了一些麻烦...我的应用程序将由另一个进程监视,但有一些异常(exception)情况,我希望用户能够在不将控制权返回给调用进程的情况下决定要做什么。这样的事情“可以接受”吗?还有其他建议吗?非常感谢!intmain(){initialize:try{//dothings}catch(...){cout 最佳答案 为什么不喜欢这样呢?while(true){//Dost

c++ - C++ 文件 IO 错误的 Try-Catch block 不起作用

我是C++错误处理领域的新手,但有人告诉我:CheckingforfileexistenceinC++...检查文件是否存在的最佳方法是使用try-catchblock。从我对该主题的有限知识来看,这听起来是个不错的建议。我找到了这段代码:http://www.java2s.com/Tutorial/Cpp/0240__File-Stream/Readafileintrycatchblock.htm#include#includeusingnamespacestd;intmain(){try{charbuffer[256];ifstreammyfile("test.txt");whil

c++ - 使用 throw() 说明符模拟方法

我正在尝试通过Google模拟一个具有throw()说明符的虚拟方法。原始函数如下所示:virtualReturnValueFunctionName()constthrow();我收到编译器错误:looserthrowspecifierfor'virtualFunctionSignature'这是我到目前为止尝试过的代码:MOCK_CONST_METHOD0(FunctionName,ReturnValue());MOCK_CONST_METHOD0(FunctionName,ReturnValue()throw());MOCK_CONST_METHOD0(FunctionName,R

c++ - 使用 throw_with_nested 并捕获嵌套异常

我真的很喜欢c++11中的std::throw_with_nested,因为它模拟了java的printStackTrace()但现在我只是好奇如何捕获嵌套异常,例如:voidf(){try{throwSomeException();}catch(...){std::throw_with_nested(std::runtime_error("Insidef()"));}}voidg(){try{f();}catch(SomeException&e){//IwanttocatchSomeExceptionhere,notstd::runtime_error,:(//dosomething

c++ - 为什么在 void 函数中接受 return throw std::exception()?

我在return之后错误地粘贴了一个throw语句,最终结果如下:voidDXManager::initialize(conststd::shared_ptr&ctx_ptr){//...if(FAILED(result)){returnthrowstd::exception("Failedtoenumeratedisplaymodelist");}//...}我在注意到错误之前成功构建了解决方案,我很好奇哪个规范允许上述语法。通过阅读cppreference.com(在注释下),我明白了Thethrow-expressionisclassifiedasprvalueexpressio

c++ - throw 或 delete 表达式可以依赖吗?

gcc5.0和clang3.6都需要typename以下示例中的关键字:templatestructB{typedefintType;};templatestructA{typedeftypenameB::TypeThrow;typedeftypenameB::TypeDelete;};C++11标准中的以下措辞涵盖了这一点:[except]/2Athrow-expressionisoftypevoid.[expr.delete]/1Theoperandshallhaveapointertoobjecttype,oraclasstypehavingasinglenon-explicit

c++ - catch 站点异常的常见用法是什么?

我对异常处理的理解非常有限。虽然我发现抛出异常很容易(或者我可以使用expected打包它供以后使用),但我对如何处理异常知之甚少。目前我的知识仅限于清理我自己的资源并在适当的位置重新抛出要处理的异常。例如ptrp=alloc.allocate(n);try{uninitialized_copy(first,last,p);//atomicgranularity,allornone}catch(...){alloc.deallocate(p,n);throw;}但我想,这可以等效地转换为RAII模式为alloc_guardp{alloc.allocate(n)};uninitializ

c++ - TRY/CATCH_ALL 与 try/catch

我使用C++有一段时间了,对普通的try/catch很熟悉。但是,我现在发现自己在Windows上,在VisualStudio中编码以进行COM开发。代码的几个部分使用了如下内容:TRY{...dostuff}CATCH_ALL(e){...issueawarning}END_CATCH_ALL;这些宏有什么意义?与内置的try/catch相比,它们有什么好处?我试过用谷歌搜索这个,但是很难搜索到“tryvsTRY”。 最佳答案 这是一个MFC宏:http://msdn.microsoft.com/en-us/library/t8d

c++ - [C++ 编译时断言] : Can we throw a compilation error if some condition is not met?

我写了一个函数:templatevoidtryHarder(){for(inti=0;i但我只希望它在N介于0和10之间时编译。我可以这样做吗?怎么办? 最佳答案 您可以使用static_assertdeclaration来完成:templatevoidtryHarder(){static_assert(N>=0&&N此功能仅在C++11之后可用。如果您坚持使用C++03,请查看Boost'sstaticassertmacro.整个想法都是很好的错误信息。如果您不关心这些,或者甚至负担不起boost,您可以执行以下操作:templa