has never used exception handling, so I am not clear about this. Recently, when I was working on a qt project, I considered adding this, but I simply wrote a program and found that
-sharpinclude "mainwindow.h"
-sharpinclude <QVBoxLayout>
-sharpinclude <QPushButton>
-sharpinclude <exception>
struct Node
{
int x;
int y;
};
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
edit = new QTextEdit;
QPushButton* btn = new QPushButton("start");
connect(btn, &QPushButton::clicked, this, &MainWindow::Slot);
resize(500, 500);
QWidget* w = new QWidget;
QVBoxLayout* lay = new QVBoxLayout;
lay->addWidget(btn);
lay->addWidget(edit);
w->setLayout(lay);
setCentralWidget(w);
}
MainWindow::~MainWindow()
{
}
void MainWindow::Slot()
{
try
{
Node* p = nullptr;
p->x = 1;
}
catch (std::exception& e)
{
edit->setText(e.what());
}
}
when the button is pressed, the Slot
function is called, but in actual operation, the textedit box does not show any abnormal information, and the program crashes and exits directly.
I Google search articles, everyone wrote, is throw, and then try...catch. Then I asked my colleague to write the same WPF program for me, and it detected it.
so I wonder: can only throw catch cPP"s exception handling mechanism?
ask for explanation.