我的 Swing 应用程序很少抛出异常。我试图捕捉完整性约束违规异常并显示消息“重复 ID”。但是当这种情况发生时,没有在这里捕获它:catch(MySQLIntegrityConstraintViolationException ex) 它会去捕获(SQLException ex)。我想要做的是,捕获 Integrity Violation 异常并显示用户友好的消息,而不是来自 ex.getMessage() 的技术消息。我该怎么做呢?
ShippmentTransfer shipTrns = new ShippmentTransfer(shipID, GIN, issueDate, ToStore, itemName, Qty, Driver, Vehicle);
int res = ShipmentTansController.addShipGIN(shipTrns);
if (res > 0) {
JOptionPane.showMessageDialog(null, "Record Added");
resetAll();
}
} catch (DataIntegrityViolationException ex) {
JOptionPane.showMessageDialog(null, "Duplicate ID");
} catch (ClassNotFoundException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex.gets);
}
}