public interface EmployeeDAO { public static final String SQL_FIND_ALL ="SELECT * FROM employees"; public List<Employee> findAll(); } public class EmployeeDAOImpl implements EmployeeDAO { private Connection conn; public EmployeeDAOImpl() throws Exception { Properties props = new Properties(); props.load(new FileInputStream("demo.properties")); String user = props.getProperty("user"); String password = props.getProperty("password"); String dburl = props.getProperty("dburl"); conn = DriverManager.getConnection(dburl, user, password); System.out.println("DB connection successful to: " + dburl); } public List<Employee> findAll() throws Exception { List<Employee> list = new ArrayList<>(); Statement stmt = null; ResultSet rs = null; try { stmt = conn.createStatement(); rs = stmt.executeQuery ... } 

And for the findAll () method, I get the following: overridden method does not throw java.lang.Exception

  • remove, from the findAll () method - throws Exception - Artem Konovalov

1 answer 1

Well, I do not see the end of this method, but I dare to assume that all operations that return exceptions are wrapped in this try / catch. Therefore, throwing in the method definition is not necessary.

If there is no forwarding exception in the interface or class that you override, then you cannot add them to the subclass.

  • The ending of the method catch (Exception ex) {System.out.println (ex); } finally {// close (stmt, rs); stmt.close (); rs.close (); } return list; Removed throws Exception, but now swears at finally block - A.Grin
  • And how to make exceptions? - A.Grin
  • So add a throw to the interface - Riĥard Brugekĥaim
  • @ A.Grin because both calls to the close method need to be wrapped in a separate try-catch. And do throws Exception - generally moveton. If any exceptions are possible in the method that it cannot handle itself, then only these exceptions should be declared, and not impersonal Exception . - Regent