简体   繁体   中英

overridden method does not throw Exception (implements in java)

I have been practicing the use of interfaces in java, but I have a small problem when I want to launch one of my classes. This is the error:

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

most of the errors that the compiler shows me come from there. I'm telling you, I'm learning the use of interfaces and I do not really know how to use them correctly.. so I do not know how I could solve this type of error.. this is my code:

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

and the Jbdc file

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

This is my interface

public interface InterfazBD{

    public void introducir();

}

The error is self-explanatory:

overridden method does not throw Exception

In your interface you have declared the method that does not throw any exception:

public void introducir();

While when implementing/overriding it, you added throws clause:

public void introducir() throws Exception {

Updating your interface method declaration to throw the same (or parent) exception will fix it.

This restriction is there because the callers of this method won't know that it can throw an exception because the variable type will be of interface, not class, like this:

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

An overriding method cannot throw more (or broader) checked exceptions than the method it is overriding. If your interface declares that it throws no checked exceptions, then any implementation also cannot declare that it throws any checked exceptions.

The compiler uses the throws clause of a method to determine if it is necessary for calling code to catch a checked exception or have the calling code itself declare that it throws the checked exception. The compiler won't allow you to circumvent this by not declaring it in the interface and declaring it in an implementing method. Even if it did, it also would be quite surprising to have a variable of type InterfazBD , which declares that it throws no checked exceptions, then at runtime, get an exception thrown that is checked, just because an implementation decided to declare that it threw that exception.

You can either declare in the interface that the method throws the same checked exception as the implementation, or you can catch and handle the exception in the implementation so that you don't need to declare it in the throws clause.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM