覆盖的方法不会抛出异常(在 java 中实现)
overridden method does not throw Exception (implements in java)
我一直在练习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();
}
错误不言自明:
overridden method does not throw Exception
在您的接口中您声明了不抛出任何异常的方法:
public void introducir();
而当 implementing/overriding 它时,您添加了 throws
子句:
public void introducir() throws Exception {
更新您的接口方法声明以抛出相同(或父)异常将修复它。
有这个限制是因为这个方法的调用者不知道它会抛出异常,因为变量类型是接口类型,而不是class,像这样:
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
覆盖方法不能抛出比它正在覆盖的方法更多(或更广泛)的已检查异常。如果您的接口声明它不抛出检查异常,那么任何实现也不能声明它抛出任何检查异常。
编译器使用方法的 throws
子句来确定调用代码是否有必要捕获已检查异常或让调用代码本身声明它抛出已检查异常。编译器不允许您通过不在接口中声明它并在实现方法中声明它来规避这一点。即使它确实如此,拥有一个 InterfazBD
类型的变量也将是非常令人惊讶的,它声明它不抛出已检查的异常,然后在运行时,抛出一个已检查的异常,只是因为实现决定声明它抛出了那个异常。
既可以在接口中声明该方法抛出与实现相同的检查异常,也可以在实现中捕获并处理异常,这样就不需要在throws
子句。
我一直在练习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();
}
错误不言自明:
overridden method does not throw Exception
在您的接口中您声明了不抛出任何异常的方法:
public void introducir();
而当 implementing/overriding 它时,您添加了 throws
子句:
public void introducir() throws Exception {
更新您的接口方法声明以抛出相同(或父)异常将修复它。
有这个限制是因为这个方法的调用者不知道它会抛出异常,因为变量类型是接口类型,而不是class,像这样:
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
覆盖方法不能抛出比它正在覆盖的方法更多(或更广泛)的已检查异常。如果您的接口声明它不抛出检查异常,那么任何实现也不能声明它抛出任何检查异常。
编译器使用方法的 throws
子句来确定调用代码是否有必要捕获已检查异常或让调用代码本身声明它抛出已检查异常。编译器不允许您通过不在接口中声明它并在实现方法中声明它来规避这一点。即使它确实如此,拥有一个 InterfazBD
类型的变量也将是非常令人惊讶的,它声明它不抛出已检查的异常,然后在运行时,抛出一个已检查的异常,只是因为实现决定声明它抛出了那个异常。
既可以在接口中声明该方法抛出与实现相同的检查异常,也可以在实现中捕获并处理异常,这样就不需要在throws
子句。