我期望子线程的状态,无论成功与否,都不应该使主线程崩溃。
所以我做了一个快速测试:
#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++,每个线程都有自己的运行时堆栈和异常进程链。
那为什么在我的例子中,子线程异常会终止主线程呢?
谢谢。
我试图在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) 以下是"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) 我正在研究这个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) 如何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) 我想知道为什么有些方法会抛出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不是吗?
大家好,我有这个构造函数
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 但它没有显示在我的代码覆盖率中。感谢帮助 !:)
为什么抛出作为引用的异常调用复制构造函数?
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)
还行吧.
在以下代码中,该函数在一条语句中引发两个异常。现在,为什么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) 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 …