标签: throw

c++11线程:子线程抛出异常导致主线程崩溃

我期望子线程的状态,无论成功与否,都不应该使主线程崩溃。

所以我做了一个快速测试:

#include <thread>
#include <iostream>
#include <exception>
using namespace std;
using namespace std::chrono;
void th_function() {
    this_thread::sleep_for(chrono::seconds(2));
    throw 1; // the criminal here!
}
int main() {
    auto th = thread{ th_function };
    this_thread::sleep_for(chrono::seconds(1));
    cout << "1" << endl;
    th.join();
    cout << "2" << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

运行结果为:

1
Program stderr
terminate called after throwing an instance of 'int'
Run Code Online (Sandbox Code Playgroud)

好吧,看来throw 1调用会导致主线程崩溃。我认为对于 C++,每个线程都有自己的运行时堆栈和异常进程链。

那为什么在我的例子中,子线程异常会终止主线程呢?

谢谢。

c++ crash multithreading exception throw

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

捕获PHP中的异常语法错误

我试图在PHP中使用异常作为避免多个if-then-else块的方法.但是,当我尝试捕获异常时,我收到错误Parse error: syntax error, unexpected T_CATCH in /directory/functions.php on line 66.我的投掷和捕捉做错了吗?

function doThis($sSchool, $sDivision, $sClass, $sUsername, $sCode,$query1,$query2) 
    {
        connectDb();
        global $dbConnection;

        $sDivisionIdArray = mysqli_query($dbConnection,$query1);
        if ($sDivisionIdArray==false){throw new Exception ();}


        $sDisplayQueryArray = mysqli_query($dbConnection,$query2);
        if ($sDisplayQueryArray==false){throw new Exception ();}

    catch (Exception $e) // This is line 666
        {echo ('Sorry, an error was encountered.');}
    }
Run Code Online (Sandbox Code Playgroud)

php exception-handling exception throw

0
推荐指数
2
解决办法
4712
查看次数

使用throw进行用户定义的异常

以下是"throw use;"显示错误的代码.为什么?如何将throw用于用户定义的异常?举个例子?

class use extends Exception{
public String toString() {
    return "too many exceptions";
}
}   
class user{
public static void main(String s[]) {
    int i=3;
    try {
        if(i>1)
            throw use;
    }
    catch(use e) {
        System.out.println(e.toString());
    }
    finally{
        System.out.println("program executed successfully!");
    }

}
}
Run Code Online (Sandbox Code Playgroud)

java exception throw

0
推荐指数
1
解决办法
8233
查看次数

C++尝试在函数中捕获Catch?

我正在研究这个C++程序.我需要使用try throw catch异常处理.我的程序编译.然而,它返回mouse没有找到.事实上它应该是laptop不应该找到的词.我已将throw代码从for循环内部移动到循环外部for.但这并没有解决结果如预期的那样.在函数中使用throw代码似乎最合乎逻辑getProductID(),但也许它应该在程序的另一部分?

#include<iostream>
#include<string>

using namespace std;

int getProductID(int ids[], string names[], int numProducts, string target)
{
      for(int i=0; i< numProducts; i++)
      {
              if (names[1] == target)
                  return ids[i];
      }
      throw(target);
}

int main() //Sample code to test the getProductID function
{
 int productIds[] = {4,5,8,10,13};
 string products[] = {"computer", "flash drive","mouse","printer","camera"};

 try
 {
      cout<< getProductID(productIds, products, 5, "mouse")<<endl;
      cout<< getProductID(productIds, products, 5, "camera")<<endl;
      cout<<getProductID(productIds, products, 5, …
Run Code Online (Sandbox Code Playgroud)

c++ function try-catch throw

0
推荐指数
1
解决办法
596
查看次数

在toString方法中抛出两个NullPointerExceptions

如何NullPointerException以相同的方法抛出两个?这就是我现在所拥有的,但第二次投掷导致错误"无法访问的声明"

public String toString() {
    if (rank == null || suit == null) {
        throw new NullPointerException ("Rank cannot be null");
        throw new NullPointerException ("Suit cannot be null");
    }
    return rank + suit;
}
Run Code Online (Sandbox Code Playgroud)

java exception throw

0
推荐指数
1
解决办法
1098
查看次数

为什么方法抛出而不是返回null?

我想知道为什么有些方法会抛出Exception而不是返回null

例如Class.forName(String className)方法抛出ClassNotFoundException.如果被调用者想要通知我们为什么不返回null,我们可以用a检查结果if,而不必写try-catch块.

为什么不写这个:

try{
    Class<?> c = Class.forName("foo.Bar");
} catch(ClassNotFoundException e){
    ...
}
Run Code Online (Sandbox Code Playgroud)

像这样:

Class<?> c = Class.forName("foo.Bar");
if(c != null) {
    ...
} else {
    //handle that class wasn't found
}
Run Code Online (Sandbox Code Playgroud)

Exceptions当可能有特定值返回(最好null或有时-1)时,为什么要使用,以通知调用者操作失败?我知道,如果一个方法可能会失败更多的方式,调用者可以在反应中以错误信息分开来,但在这种情况下,ClassNotFoundException只有当类找不到抛出.

而且还有性能问题,返回的速度要快null于抛出Exception不是吗?

java exception throw

0
推荐指数
1
解决办法
124
查看次数

EXPECT_THROW - 实际:它抛出不同的类型,谷歌测试

大家好,我有这个构造函数

Matrix::Matrix(size_t row, size_t col)
{
  if(row < 1 || col < 1)
    throw new std::runtime_error("Minimalni velikost matice je 1x1");
  matrix = std::vector<std::vector< double > >(row,std::vector<double>(col, 0));
}
Run Code Online (Sandbox Code Playgroud)

和这个测试

Matrix *TestedMatrix;
EXPECT_THROW(TestedMatrix = new Matrix(-2,3),std::runtime_error );
Run Code Online (Sandbox Code Playgroud)

但我仍然得到那个例外是不同类型的。我也试过,std::runtime_error*但结果是一样的。一开始我想使用 EXPECT_ANY_THROW 但它没有显示在我的代码覆盖率中。感谢帮助 !:)

c++ matrix googletest throw c++11

0
推荐指数
1
解决办法
5945
查看次数

为什么抛出作为引用的异常调用复制构造函数?

为什么抛出作为引用的异常调用复制构造函数?

struct Error
{
    Error() {}
    Error(const Error&) = delete;
};

int main()
{
    Error& error = *new Error;

    throw error;
}
Run Code Online (Sandbox Code Playgroud)

编译错误:

error: declared here
     Error(const Error&) = delete;
Run Code Online (Sandbox Code Playgroud)

投掷指针时不会发生这种情况:

int main()
{
    Error* error = new Error;

    throw error;
}
Run Code Online (Sandbox Code Playgroud)

还行吧.

c++ exception copy-constructor throw

0
推荐指数
1
解决办法
103
查看次数

引发多个异常时catch块执行的顺序是什么?为什么?

在以下代码中,该函数在一条语句中引发两个异常。现在,为什么int catch块处理异常而不是其他块?是否总是存在最后一个异常是要处理的异常的情况?


    try
    {
        quotient = safe_divide(numerator , denominator);
    }
    catch(DivideByZero)
    {
        cout << "Error: Division by zero!\n"
            << "Program aborting.\n";
        system("pause");
    }

    catch (int )
    {
        cout << "got you " << endl;
        cout << "top : " << numerator << endl;

        system("Pause");
        exit(0);
    }

    double safe_divide(int top, int bottom) throw(DivideByZero,int)
    {
        if(bottom == 0)
            throw (DivideByZero(),top);

        return top/static_cast<double>(bottom);
    }


Run Code Online (Sandbox Code Playgroud)

c++ exception try-catch throw

0
推荐指数
1
解决办法
68
查看次数

Should I reuse the same error number in different stored procedures when throwing errors?

I'm using Giraffe framework to implement a web server in F#. The web server executes some stored procedures based on client requests. Errors are thrown inside stored procedures when the request cannot be processed and I'm catching the errors in Giraffe like this:

try
     // parse request ...
    // data access and execute stored procdeure ...
with
| :? System.Data.SqlClient.SqlException as e ->
     match e.Number with
    | 60000 ->
        return! (setStatusCode 400 >=> json e.Message) next ctx
Run Code Online (Sandbox Code Playgroud)

Let's say …

sql-server f# throw

0
推荐指数
1
解决办法
41
查看次数