gpt4 book ai didi

java - 重写的方法不会抛出异常(在java中实现)

转载 作者:行者123 更新时间:2023-12-02 01:38:46 39 4
gpt4 key购买 nike

我一直在练习在java中使用接口(interface),但是当我想启动我的一个类时遇到了一个小问题。这是错误:

./Jbdc.java:18: error: introducir() in Jbdc cannot implement introducir()  in InterfazBD
public void introducir() throws Exception

overridden method does not throw Exception
1 error

编译器向我显示的大多数错误都来自那里。我告诉你,我正在学习接口(interface)的使用,但我真的不知道如何正确使用它们..所以我不知道如何解决此类错误..这是我的代码:

 (Principal)
public class App{

public static void main(String arg[]){

JsonRead json = new JsonRead();
Jbdc sqlite = new Jbdc();
grabarJson(json);
introducirSQL(sqlite);
}

public static void grabarJson(InterfazGrabar fichero){
fichero.grabar();
}

public static void grabarTxt(InterfazGrabar fichero){
fichero.grabar();
}

public static void introducirSQL(InterfazBD fichero){
fichero.introducir();
}
}

和 Jbdc 文件

 Jbdc (this file enter the data in a database)

public class Jbdc implements InterfazBD{

private static final String URL = "jdbc:sqlite:test.db";
public void introducir() throws Exception{
createDb();
createTable();
Aula a = null;
int contador = 0;
try {
FileInputStream inFile = new FileInputStream("aula.dat");
ObjectInputStream in = new ObjectInputStream(inFile);
while (inFile.available()>0) {
a = (Aula)in.readObject();
String materiaslista ="";
String nombre = a.getNombre();
String grupo = a.getGrupo();
int tutor= a.getTutor();
ArrayList<String> materias = a.getMaterias();
for (int counter = 0; counter < materias.size(); counter++) {
materiaslista = materiaslista + materias.get(counter) + ",";
}
insertDatos(nombre,grupo,tutor,materiaslista);
}

}
catch(IOException e)
{
System.err.println("ERROR");
}
System.out.println("¡Listo!");
}

private static void insertDatos(String nombre,String grupo, int tutor,String materiaslista) {
final String SQL = "INSERT INTO datos VALUES(?,?,?,?)";
try (Connection con = getConnection(); PreparedStatement ps = con.prepareStatement(SQL);) {
ps.setString(1, nombre);
ps.setString(2, grupo);
ps.setInt(3, tutor);
ps.setString(4, materiaslista);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}

private static void createTable() {
final String SQL = "CREATE TABLE IF NOT EXISTS datos (nombre TEXT,grupo TEXT, tutor INTEGER, materiaslista TEXT);";
// This SQL Query is not "dynamic". Columns are static, so no need to use
// PreparedStatement.
try (Connection con = getConnection(); Statement statement = con.createStatement();) {
statement.executeUpdate(SQL);
} catch (SQLException e) {
e.printStackTrace();
}
}

private static void createDb() {
try (Connection conn = getConnection()) {
if (conn != null) {
conn.getMetaData();
}
} catch (SQLException e) {
e.printStackTrace();
}
}

public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL);
}
}

这是我的界面

public interface InterfazBD{

public void introducir();

}

最佳答案

错误是不言自明的:

overridden method does not throw Exception

在您的界面中,您已声明不引发任何异常的方法:

public void introducir();

在实现/重写它时,您添加了 throws 子句:

public void introducir() throws Exception {

更新接口(interface)方法声明以引发相同(或父级)异常即可修复该问题。

存在此限制是因为此方法的调用者不知道它会抛出异常,因为变量类型将是接口(interface),而不是类,如下所示:

MyInterface x = new MyClass(); //MyClass implements MyInterface
x.someMethod(); //guaranteed that its implementation in MyClass won't throw an exception if its declaration in MyInterface doesn't throw exception

关于java - 重写的方法不会抛出异常(在java中实现),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54776413/

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