当没有待处理的异常在堆栈中处理更高时,C++标准对于以下代码应该说什么呢?
try {
throw;
} catch (...) {
cerr << "Caught exception." << endl;
}
Run Code Online (Sandbox Code Playgroud)
没有任何对象的投掷会被抓住吗?
有人可以描述一下处理以下情况的正确方法:
wchar_t* buffer = new wchar_t[...];
if (!something)
{
throw std::runtime_error("Whatever");
// Now, at this point I'm leaking memory allocated for the 'buffer'.
}
// Perform actions.
delete[] buffer;
Run Code Online (Sandbox Code Playgroud)
解决它的明显方法意味着:
if (!something)
{
delete[] buffer;
throw std::runtime_error("Whatever");
}
Run Code Online (Sandbox Code Playgroud)
现在 - 好吗?(我怀疑是这样,但谁知道:)
PS我确实意识到有一种更好的方法 - 使用boost::scoped_array或简单地std::wstring允许被调用的析构函数释放已分配的内存,只是好奇.
在C++中,您可以使用如下的异常规范声明函数:
int foo() const throw(Exception);
Run Code Online (Sandbox Code Playgroud)
我找到了这两个链接:
但有几件事最终没有答案......
问题1:为什么要添加异常规范?它会带来任何性能提升吗?编译器会有什么不同?因为它对我来说似乎就像程序员的信息.
问题2:如果我扔掉一些不符合规格的东西会发生什么(会发生什么)?例如:
int foo() throw(int) {
throw char; // Totally unrelated classes, not types in real
}
Run Code Online (Sandbox Code Playgroud)
问题3:函数/方法不应该抛出任何东西.我找到了至少两个(三种,不同编译器的替代语法)方法来指定无异常抛出:
int foo() throw();int foo() __attribute(nothrow)__ 对于gccint foo() nothrow 用于Visual C++哪一个是"正确的"?有什么区别吗?我应该使用哪一个?
问题4: " 非标准例外" bad_alloc,bad_cast,bad_exception,bad_typeid和ios_base::failure.
好的bad_alloc是自我解释,我知道如何(更重要的是何时)使用它(添加到异常规范),但其他的呢?他们都没有真正响铃......他们与哪些"代码片段"相关联?喜欢bad_alloc与之相关new char[500000].
问题5:如果我有异常类层次结构,如下所示:
class ExceptionFileType {
virtual const char * getError() const = 0;
};
class …Run Code Online (Sandbox Code Playgroud) 我想知道Java如何采用以下方案
public static void main(String[] args) throws IndexOutOfBoundsException, CoordinateException, MissionException, SQLException, ParserConfigurationException {
try {
doSomething();
} catch (Exception e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我声明了抛出许多不同异常的main函数,但在函数内部,我正在捕获泛型异常.我想知道java内部如何使用它?即,doSomething()抛出一个IndexOutOfBounds异常,e.printStackTrace()会在最后一个catch (Exception e) {...}块中被调用吗?
我知道如果函数的throws区域中没有声明的异常被抛出,try/catch将处理它,但是声明中提到的异常呢?
通常它会在范围结束时被破坏.但是如果抛出异常,我可以看到问题.
对于下面的方法,当它不在“try”块内时是否可以抛出异常?
public void setCapacity(x) throws CapacityExceededException {
if (x > 10) throw new CapacityExceededException(x);
else this.x = x;
}
Run Code Online (Sandbox Code Playgroud) 我目前正在开发REST服务并BadRequestException为以下所有内容投掷,
1. Path parameter is invalid
2. Query parameter is invalid
4. Input request object has missing attributes
Run Code Online (Sandbox Code Playgroud)
对于每种情况,是否有任何特定的例外,例如InvalidParameterException等等......?是否有任何文档可以了解在什么情况下应该抛出哪些异常?
我收到以下错误:
Throwing method cannot be a member of an @objc protocol because it returns a value of type 'Bool'; return 'Void' or a type that bridges to an Objective-C class
Run Code Online (Sandbox Code Playgroud)
在定义 Swift 协议时也需要桥接到 Objective-C:
@objc public protocol Saving {
func save() throws -> Bool
}
Run Code Online (Sandbox Code Playgroud)
有没有其他方法来定义 Swift 方法,它可以返回Bool,可能会抛出错误并Objetive-C兼容?
从这个答案/sf/answers/2571688381/:
没有操作数的throw-expression重新抛出当前处理的异常.使用现有临时值重新激活该例外; 没有创建新的临时异常对象. - ISO/IEC 14882:2011第15.1节.8
那么为什么我从这段代码中得到这个结果呢?
码:
#include <iostream>
class my_exception: public std::exception{
public:
int value;
};
int main()
{
my_exception ex;
ex.value=1;
try{
throw ex;
}
catch(my_exception& e){
e.value=2;
}
std::cout << ex.value;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
实际结果:
1
我认为它应该是2,取决于标准配额.我错过了什么?
实例MonadPlus IO是唯一的,因为mzero抛出:
Prelude Control.Monad> mzero
*** Exception: user error (mzero)
Run Code Online (Sandbox Code Playgroud)
因此,相应地,这也MonadPlus IO 意味着它也适用于错误。
mzero 如果其他动作不抛出,则显然用作标识元素:
Prelude Control.Monad> mzero `mplus` return 0
0
Prelude Control.Monad> return 0 `mplus` mzero
0
Run Code Online (Sandbox Code Playgroud)
但是当两个动作都抛出时不是这样:
Prelude Control.Monad> fail "Hello, world!" `mplus` mzero
*** Exception: user error (mzero)
Prelude Control.Monad> mzero `mplus` fail "Hello, world!"
*** Exception: user error (Hello, world!)
Run Code Online (Sandbox Code Playgroud)
所以MonadPlus IO不是monoid。
如果MonadPlus在用户意图出错时违反法律,那么它的实际意图是什么?