gpt4 book ai didi

java - 没有未捕获/抛出异常的编译器错误/警告

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:35:30 24 4
gpt4 key购买 nike

我不明白为什么编译器不警告我没有捕获或抛出 SQLException。情况是这样的:

我已经定义了这个接口(interface):

public interface GenericDatabaseManager {
public void createTables(DataBase model) throws SQLException;
}

然后我创建了这个实现给定接口(interface)的类:

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 不需要捕获,因为它是一个RuntimeExceptionThis 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(); 中显示错误。

最佳答案

android.database.SQLException是运行时异常。

在 Java 中,没有必要捕获或声明运行时异常的抛出。阅读有关 RuntimeExceptions 的详细说明在 Java 中 here

关于java - 没有未捕获/抛出异常的编译器错误/警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13509829/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com