标签: throw

如果我试图在C++中的catch块中抛出一些内容,为什么它会导致终止?

我有以下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)

c++ exception try-catch throw gcc4

10
推荐指数
2
解决办法
5440
查看次数

捕获 SwiftUI 中的错误

我在某些视图中有一个按钮,它调用 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 中的错误?

try-catch throw ios swiftui

10
推荐指数
1
解决办法
7707
查看次数

可以/应该在ac#switch语句中抛出异常吗?

我有一个返回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# exception throw switch-statement

9
推荐指数
2
解决办法
3841
查看次数

抛出异常并从函数返回结果

我只是学习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)

我该怎么办?返回一个空的向量?如果我将异常抛给返回值的接收器会发生什么?

c++ exception throw

9
推荐指数
1
解决办法
2万
查看次数

有什么问题或者抛出新的例外?

我使用的是里面的这段代码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_USERBASE_DB_PASS定义.我收到以下错误:

解析错误:语法错误,第6/ home/...中的意外T_THROW

我不允许使用or带例外的结构吗?我该如何解决这个问题?

php exception throw parse-error

9
推荐指数
1
解决办法
4252
查看次数

c#.net中throw和throw ex之间的差异

谁能告诉我throwthrow ex简短的区别?我读过那个throw存储以前的异常,没有得到这一行.
我能举例说明这个吗?

c# throw

9
推荐指数
2
解决办法
3万
查看次数

具有/不具有throw/noexcept功能的潜在C++编译器优化

假设以下课程:

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;表达式.另一方面,如果编译器将其优化并 …

c++ exception throw compiler-optimization

9
推荐指数
1
解决办法
312
查看次数

与g ++ 6.2不同的异常说明符

有人可以向我解释为什么这个代码不能用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)

c++ g++ exception throw

9
推荐指数
1
解决办法
5153
查看次数

swift lazy var with throw init behavior

我不确定它是不是一个错误,或者它确实是应该如何运作的?

class A {
    init() throws { }
}

class B {
    lazy var instance = A()
}
Run Code Online (Sandbox Code Playgroud)

这段代码使用XCode 9和最新Swift版本编译没有错误,并且除非Class A init()真正抛出,否则工作完美,然后lazy var为空指针.但是不应该以某种方式编译这段代码?

throw lazy-initialization swift

9
推荐指数
1
解决办法
811
查看次数

我们应该提供一个没有抛出说明符的析构函数吗?

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说明符的空实现.

问题:这有必要吗?或者这是一个很好的做法,比较让编译器生成隐式析构函数?

c++ destructor exception throw specifier

8
推荐指数
2
解决办法
2838
查看次数