我有一个Database
和一个Control
类。 Database 类
通过提供创建 SQLite 数据库和插入数据等方法,使访问 SQLite 数据库变得更加容易。这些方法可能会抛出 SqlJetException,每个方法都会处理该异常。
Control 类
正在从Database 类
调用这些方法,但提示未捕获SqlJetException。我个人认为 Control 类中的 SqlJetException 处理是糟糕的面向对象设计,因此我想避免这种情况。
我的问题是为什么我的数据库方法会抛出异常,尽管我已经捕获了异常?我该如何解决这个问题?
错误日志
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ voc ---
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 2 source files to /home/toogley/src/voc/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /home/toogley/src/voc/src/main/java/voc/Control.java:[14,23] error: unreported exception SqlJetException; must be caught or declared to be thrown
[ERROR] /home/toogley/src/voc/src/main/java/voc/Control.java:[15,25] error: unreported exception SqlJetException; must be caught or declared to be thrown
[ERROR] /home/toogley/src/voc/src/main/java/voc/Control.java:[16,21] error: unreported exception SqlJetException; must be caught or declared to be thrown
[ERROR] /home/toogley/src/voc/src/main/java/voc/Control.java:[20,21] error: unreported exception SqlJetException; must be caught or declared to be thrown
[INFO] 4 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
数据库类:
package voc;
import org.tmatesoft.sqljet.core.*;
import org.tmatesoft.sqljet.core.table.ISqlJetTable;
import org.tmatesoft.sqljet.core.table.SqlJetDb;
import java.io.File;
public class Database {
private SqlJetDb db;
public void createNew() throws SqlJetException {
File dbFile = new File("db.sqlite");
dbFile.delete();
try {
db = SqlJetDb.open(dbFile, true);
db.getOptions().setAutovacuum(true);
db.beginTransaction(SqlJetTransactionMode.WRITE);
db.getOptions().setUserVersion(1);
} catch (SqlJetException e) {
e.printStackTrace();
} finally {
db.commit();
}
}
public void createTable() throws SqlJetException {
try {
db.beginTransaction(SqlJetTransactionMode.WRITE);
db.createTable("voc");
db.createIndex("english-term");
db.createIndex("german-translation");
} catch (SqlJetException e) {
e.printStackTrace();
} finally {
db.commit();
}
}
public void setData(String englishVoc, String germanTranslation) throws SqlJetException {
try {
db.beginTransaction(SqlJetTransactionMode.WRITE);
ISqlJetTable table = db.getTable("voc");
table.insert(englishVoc, germanTranslation);
} catch (SqlJetException e) {
e.printStackTrace();
} finally {
db.commit();
}
}
public String getData(String s) throws SqlJetException {
String returnVal = "";
try {
db.beginTransaction(SqlJetTransactionMode.READ_ONLY);
ISqlJetTable table = db.getTable("voc");
returnVal = table.lookup(s).toString();
} catch (SqlJetException e) {
e.printStackTrace();
} finally {
db.commit();
}
return returnVal;
}
控制类
package voc;
public class Control {
private Database theDB;
public Control() {
theDB = new Database();
}
public void fillDB() {
theDB.createNew(); // <<< SqlJetException
theDB.createTable(); // <<< SqlJetException
theDB.setData("mobility", "Ortsunabhängigkeit"); // <<< SqlJetException
}
public void receiveData() {
theDB.getData("test"); // <<< SqlJetException
}
}
最佳答案
如果您捕获了所有这些,则您的方法抛出 SqlJetException 就不再正确,您应该删除方法签名的该部分。
编译器实际上并不理解这种情况,当你声明你的方法抛出这个和那个检查异常时,编译器不会进一步检查它是否为真。只有相反的情况才会被捕获,当您的代码实际上可能会抛出检查异常,并且既没有捕获也没有在方法签名中声明时,您将从编译器收到错误消息。
关于java - 捕获 SqlJetException 的方法在执行后抛出 SqlJetException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36103019/