格式化文件异常被 try catch 块忽略
Format File Exception ignored by the try catch block
我必须通过使用Windows资源管理器选择文件来从XML文件加载数据。如果我选择其他格式,而不是 XML,则 catch 块应该会出现异常。但它没有,应用程序失败。这是我到目前为止编写的代码。我错过了什么?我将表达式从 xml 方法扔到readVehicles(QString fileName)
中。
QList<Vehicle> VehicleHelper::readVehicles(QString fileName){
QList<Vehicle> vehicles;
Vehicle newVehicle;
QFile file(fileName);
QDomDocument document;
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
throw FileCannotBeOpenException();
}
else{
if(!document.setContent(&file)){
throw InvalidXMLContentException();
}
file.close();
}
QDomElement root = document.firstChildElement();
QDomNodeList carElements = root.elementsByTagName("Car");
for(int i = 0; i < carElements.size(); i++){
QDomNode carNode = carElements.at(i);
QDomElement carElement = carNode.toElement();
QString carID = carElement.attribute("ID");
//if the idNumber is null, generate one
if(carID.isEmpty()){
QUuid newId = Vehicle::generateID();
newVehicle.setVehicleId(newId);
}
else {
QUuid id;
try{
id = QUuid::QUuid(carID);
} catch(QException &ex){
continue;
}
newVehicle.setVehicleId(id);
}
//Owner
//****************************************************************
QDomElement ownerElement = carNode.firstChildElement("Owner");
//FirstName
QDomElement fNameElement = ownerElement.firstChildElement("FirstName");
QString fName = fNameElement.text();
//LastName
QDomElement lNameElement = fNameElement.nextSiblingElement("LastName");
QString lName = lNameElement.text();
//Address
QDomElement addressElement = lNameElement.nextSiblingElement("Address");
QString address = addressElement.text();
//append vehicle to the list
vehicles.append(newVehicle);
}
}
并且 try-catch 块在 on_actionOpen_triggered()
方法中
void MainWindow::on_actionOpen_triggered()
{
if(isModified){
QMessageBox msgBox;
QString message = "There are unsaved changes! Do you proceed? ";
msgBox.setWindowTitle("Save Changes");
msgBox.setText(message);
msgBox.addButton("Don't save",QMessageBox::NoRole);
msgBox.addButton(QMessageBox::Save);
msgBox.addButton(QMessageBox::Cancel);
msgBox.setIcon(QMessageBox::Question);
int result = msgBox.exec();
if(result == QMessageBox::Save){
VehicleHelper::writeVehicles(cars,filename);
msgBox.close();
} else {
QString fileName = QFileDialog::getOpenFileName(this,"Open file");
if(!fileName.isEmpty()){
setFileName(fileName);
QFile file(fileName);
try {
cars = VehicleHelper::readVehicles(fileName);
} catch(FileCannotBeOpenException &ex) {
QMessageBox msgBox;
msgBox.setIcon(QMessageBox::Critical);
msgBox.setWindowTitle("Message!");
msgBox.setText("Failed to open file");
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.exec();
return;
} catch(InvalidXMLContentException &ex){
QMessageBox msgBox;
msgBox.setIcon(QMessageBox::Critical);
msgBox.setWindowTitle("Message!");
msgBox.setText("Failed to load data!");
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.exec();
return;
}
setTakenNumbersList(cars);
//set data to the table view
populate(cars);
}
}
} else {
QString fileName = QFileDialog::getOpenFileName(this,"Open file");
if(!fileName.isEmpty()){
setFileName(fileName);
QFile file(fileName);
if(file.open(QFile::ReadOnly | QFile::Text)){
cars = VehicleHelper::readVehicles(fileName);
setTakenNumbersList(cars);
file.close();
//set data to the table view
populate(cars);
ui->actionAdd->setEnabled(true);
ui->actionBy_name->setEnabled(true);
ui->actionBy_registration_date->setEnabled(true);
ui->actionBy_registration_number->setEnabled(true);
ui->actionBy_revision_date->setEnabled(true);
ui->actionBy_type->setEnabled(true);
ui->actionClear_Search->setEnabled(true);
ui->actionDelete->setEnabled(true);
ui->actionEdit->setEnabled(true);
ui->actionSave->setEnabled(true);
ui->actionSave_As->setEnabled(true);
}
}
}
}
在将其发送到QFile
之前,您必须检查QString
中是否".xml"
最后四个字符。
相关文章:
- 编译器是否必须始终删除 try-catch 块(如果它被证明是非抛出的)
- 有没有更好的方法来处理异常? try-catch块真的很丑
- 我可以使用 try catch 语句来捕获任何错误而不是具体错误吗?
- 在大型应用程序的main上使用try-catch
- RapidXML 节点在 try catch 块中具有正确的值,但它在块外为 nullptr
- 提取 try-catch 时出现运行时错误
- std::unique_ptr 在 try-catch 块中未捕获取消引用异常
- 何时删除 try-catch 块中的指针
- 是否有理由大多数/所有 try-catch 示例只对 throw 语句使用 void 子函数
- 循环try..catch ..确保输入有效或使用控制语句
- 构造函数中的异常:init() 方法、指针、大型 try/catch 或
- C++ C2509 在讲师的定义中使用 try: - catch
- 为什么未达到的 try-catch 块会增加运行时时间
- 在 C++ 中使用 try-catch 进行删除 [] 或删除操作是否必要
- ex.nested try-catch中的什么()更改
- 为什么 c++ try-catch 块不能在 ubuntu 上与 g++ 一起使用?
- 格式化文件异常被 try catch 块忽略
- libpng错误不会被try/catch块捕获
- 在 C++ 和 C# 中使用 try/catch 的正确方法是什么?
- 如何使用catch try-catch