我有以下C++代码,它给了我一个惊喜.问题是如果我抛出一些东西,除了在catch块内重新抛出,程序将通过调用abort终止并在GCC4中给出错误消息,"在抛出'int'实例后调用终止".如果我只是用"扔"; 重新扔进catch区,一切都会好的.
#include <iostream>
#include <exception>
#include <stdexcept>
using namespace std;
int main()
{
try{
throw std::string("first throw");
}
catch(std::string &x){
try{
std::cout << x << std::endl;
// throw; // if I use this line, all is fine.
throw int(2); // but if I use this line, it causes Abort() to be called
}
catch (int &k){
throw;
}
catch(...)
{
cout << "all handled here!"<< endl;
}
}
catch(...){
std::cout<< "never printed" << endl;
}
}
Run Code Online (Sandbox Code Playgroud) 我在某些视图中有一个按钮,它调用 ViewModel 中可能引发错误的函数。
Button(action: {
do {
try self.taskViewModel.createInstance(name: self.name)
} catch DatabaseError.CanNotBeScheduled {
Alert(title: Text("Can't be scheduled"), message: Text("Try changing the name"), dismissButton: .default(Text("OK")))
}
}) {
Text("Save")
}
Run Code Online (Sandbox Code Playgroud)
try-catch 块产生以下错误:
Invalid conversion from throwing function of type '() throws -> Void' to non-throwing function type '() -> Void'
Run Code Online (Sandbox Code Playgroud)
这是 viewModel 中的 createInstance 函数,taskModel 函数以同样的方式处理错误。
func createIntance(name: String) throws {
do {
try taskModel.createInstance(name: name)
} catch {
throw DatabaseError.CanNotBeScheduled
}
}
Run Code Online (Sandbox Code Playgroud)
如何正确捕获 SwiftUI 中的错误?
我有一个返回int的插入查询.基于该int我可能希望抛出异常.这是否适合在switch语句中执行?
switch (result)
{
case D_USER_NOT_FOUND:
throw new ClientException(string.Format("D User Name: {0} , was not found.", dTbx.Text));
case C_USER_NOT_FOUND:
throw new ClientException(string.Format("C User Name: {0} , was not found.", cTbx.Text));
case D_USER_ALREADY_MAPPED:
throw new ClientException(string.Format("D User Name: {0} , is already mapped.", dTbx.Text));
case C_USER_ALREADY_MAPPED:
throw new ClientException(string.Format("C User Name: {0} , is already mapped.", cTbx.Text));
default:
break;
}
Run Code Online (Sandbox Code Playgroud)
我通常会向交换机添加break语句,但它们不会被命中.这是一个糟糕的设计吗?请与我分享任何意见/建议.
谢谢,〜在圣地亚哥
我只是学习C++,并想抛出异常,但是我的函数的结果将是未定义的???
std::vector<myStruct> extract_notworking(std::vector<myStruct>& avec){
std::vector<myStruct> result;
if (avec.size() == 0)
//throw domain_error("Cannot operate on empty vector!");
//Cannot use exception for it would yield undefined result
return result;
//do something here
//...
return result;
}
Run Code Online (Sandbox Code Playgroud)
我该怎么办?返回一个空的向量?如果我将异常抛给返回值的接收器会发生什么?
我使用的是里面的这段代码public function __construct()
的class
:
$this->mConnection = mysql_connect(BASE_DB_HOST,BASE_DB_USER,BASE_DB_PASS) or throw new Exception("Couldn't connect to database.");
Run Code Online (Sandbox Code Playgroud)
BASE_DB_HOST
,BASE_DB_USER
并BASE_DB_PASS
定义.我收到以下错误:
解析错误:语法错误,第6行/ home/...中的意外T_THROW
我不允许使用or
带例外的结构吗?我该如何解决这个问题?
谁能告诉我throw
和throw ex
简短的区别?我读过那个throw
存储以前的异常,没有得到这一行.
我能举例说明这个吗?
假设以下课程:
class Example
{
public:
...
Example& operator=(const Example& rhs);
...
private:
other_type *m_content;
size_t m_content_size;
}
Example& Example::operator=(const Example& rhs)
{
if (this != &rhs)
{
delete m_content;
m_content = nullptr;
m_content = getCopiedContent(rhs);
}
return *this;
}
Run Code Online (Sandbox Code Playgroud)
我知道这不是最好的实现方式,operator=
但这是有目的的,因为我的问题是关于这两行:
m_content = nullptr;
m_content = getCopiedContent(rhs);
Run Code Online (Sandbox Code Playgroud)
可以是编译器将优化,m_content = nullptr;
即使getCopiedContent
未定义为throw()
或noexcept
:
other_type* getCopiedContent(const Example& obj);
Run Code Online (Sandbox Code Playgroud)
一方面,编译器可以假设如果在m_content = nullptr;
我m_content
用返回值覆盖值之后getCopiedContent
,它可以优化整个m_content = nullptr;
表达式.另一方面,如果编译器将其优化并 …
有人可以向我解释为什么这个代码不能用g ++版本6.2.0编译,但是用clang ++版本3.9.0-svn274438-1和icpc版本16.0.2编译得很好
$ cat wtf.cpp
#include <cstdio>
#include <new>
void *operator new(std::size_t) throw(std::bad_alloc);
void *operator new(std::size_t) throw (std::bad_alloc) { void *p; return p; }
$ g++-6 wtf.cpp -c
wtf.cpp: In function ‘void* operator new(std::size_t)’:
wtf.cpp:4:7: error: declaration of ‘void* operator new(std::size_t) throw (std::bad_alloc)’ has a different exception specifier
void *operator new(std::size_t) throw (std::bad_alloc) { void * p; return p; }
^~~~~~~~
wtf.cpp:3:7: note: from previous declaration ‘void* operator new(std::size_t)’
void *operator new(std::size_t) throw(std::bad_alloc);
Run Code Online (Sandbox Code Playgroud) 我不确定它是不是一个错误,或者它确实是应该如何运作的?
class A {
init() throws { }
}
class B {
lazy var instance = A()
}
Run Code Online (Sandbox Code Playgroud)
这段代码使用XCode 9和最新Swift
版本编译没有错误,并且除非Class A
init()
真正抛出,否则工作完美,然后lazy var为空指针.但是不应该以某种方式编译这段代码?
namespace QuantLib {
//! Base error class
class Error : public std::exception {
public:
/*! The explicit use of this constructor is not advised.
Use the QL_FAIL macro instead.
*/
Error(const std::string& file,
long line,
const std::string& functionName,
const std::string& message = "");
/*! the automatically generated destructor would
not have the throw specifier.
*/
~Error() throw() {}
//! returns the error message.
const char* what() const throw ();
private:
boost::shared_ptr<std::string> message_;
};
}
Run Code Online (Sandbox Code Playgroud)
正如您在注释中看到的那样,类的析构函数Error
显式提供了一个带有no-throw说明符的空实现.
问题:这有必要吗?或者这是一个很好的做法,比较让编译器生成隐式析构函数?