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

问题描述 投票:0回答:2

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

./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

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

 (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();

}
java sqlite implements
2个回答
2
投票

错误是不言自明的:

重写方法不会抛出异常

在您的界面中,您已声明了不抛出任何异常的方法:

public void introducir();

在实现/覆盖它时,您添加了throws子句:

public void introducir() throws Exception {

更新接口方法声明以抛出相同(或父)异常将修复它。

这种限制是因为这个方法的调用者不会知道它可以抛出异常,因为变量类型将是接口而不是类,如下所示:

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

0
投票

覆盖方法不能抛出比覆盖它的方法更多(或更广泛)的已检查异常。如果你的接口声明它没有抛出任何已检查的异常,那么任何实现也都不能声明它抛出任何已检查的异常。

编译器使用方法的throws子句来确定是否有必要调用代码来捕获已检查的异常,或者让调用代码本身声明它是否会抛出已检查的异常。编译器不会允许您通过不在接口中声明它并在实现方法中声明它来绕过这一点。即使它确实如此,拥有一个InterfazBD类型的变量也是相当令人惊讶的,它声明它不会抛出任何已检查的异常,然后在运行时,抛出一个被检查的异常,只是因为一个实现决定声明它抛出了那个例外。

您可以在接口中声明方法抛出与实现相同的已检查异常,或者您可以在实现中捕获并处理异常,这样您就不需要在throws子句中声明它。

© www.soinside.com 2019 - 2024. All rights reserved.