我不明白爲什麼編譯器不會警告我沒有捕獲或拋出SQLException
。這裏的情況:沒有捕獲/拋出異常沒有編譯器錯誤/警告
我已經定義了這個接口:
public interface GenericDatabaseManager {
public void createTables(DataBase model) throws SQLException;
}
然後,我創建了這個類實現給定接口:
public class SqliteHelper extends SQLiteOpenHelper implements
GenericDatabaseManager {
@Override
public void createTables(DataBase model) throws SQLException {
// Code that throws SQLException
}
最後我打電話這個SqliteHelper.createTables( )從這裏開始:
public class DatabaseManager extends CoreModule {
private boolean createUpdateDB(final String dbString, final String appId) {
// Previous code...
if (oldVer == -1) {
dbCoreModel.addModel(dbModel);
dbCoreModel.getManager().createTables(dbModel);
return true;
}
// More code...
}
}
dbCoreModel.getManager()
返回GenericDatabaseManager
實例。但編譯器在dbCoreModel.getManager().createTables(dbModel);
行顯示沒有錯誤,但該行會拋出SQLException
。
有沒有人有一個想法,爲什麼會發生這種情況?提前致謝。
編輯:約SQLException
不需要捕捉,因爲它是RuntimeException
。 This is not true。這裏有一個例子:
import java.sql.SQLException;
interface Interface {
public void throwsSQLException() throws SQLException;
}
class Test implements Interface {
@Override
public void throwsSQLException() throws SQLException {
throw new SQLException();
}
}
public class Main {
public static void main(String[] args) {
Interface i = new Test();
i.throwsSQLException();
System.out.println("Finished");
}
}
編譯器在這種情況下顯示i.throwsSQLException();
錯誤。
無用的問:「Previous code ...」是否包含「try {」和「More code ...」包含一個'} catch(SQLException e){',right ? :D –
是的,沒有嘗試/捕獲當然 – m0skit0
然後,作爲PC。回答,你沒有使用'java.sql.SQLException',而是'android.database.SQLException'。檢查你的'進口'。 –