Note that there are some explanatory texts on larger screens.

plurals
  1. POis there a way to both catch and throw an exception
    text
    copied!<p>I have a method that is going to the DB so all our JDBC stuff in the DAO method is inside a try/catch block. It is catching <code>SQLException</code></p> <p>When I write a test case against this method and if a <code>SqlException</code> occurs then my testcase does not say 'caused an error'. it just goes on its merry way. </p> <p>However, If I dont catch the SqlException in the DAO method but add <code>throws SqlException</code> to the method instead then my test case says 'caused an error' and shows me the error that happened. (this is what I want).</p> <p>When I try adding throws <code>SqlException</code> along with the method catching the <code>SqlException</code> then also my test case does not say 'caused an error'.</p> <p>what is the way around it? one is to have try/catch block inside my test case but even if I do this my Junit test case does not say 'caused an error' though the exception IS posted to standard output. </p> <p>...but besides that anything else?</p> <p>My IDE is Netbeans. this is where I run the test cases. </p> <p>Code:</p> <pre><code>public class MyDaoClass { Connection con; public MyDaoClass (Connection connection) { this.con = connection; } public SomeObject someMethod (String id) { try{ Connection con = this.con; CallableStatement cs = con.prepareCall("{call some_sp_name (?)}"); cs.setString (1, id); cs.execute()//imagine an error happens here ResultSet rs = cs.getResultSet() ... .... //return SomeObject... } catch (SqlException e) //If I remove this and add 'throws SQLException to method then everything is ok { log.error(e.getMessage());//i dont have access to log object in test case } } } public class MyTestSuite extends TestCase { //populate local connection public void testSomeMethod () throws SQLException { MyDaoClass myd = new MyDaoClass(connection); SomeObject s = myd.someMethod("blah"); assertEquals (s.getFirstName(), "pepe"); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload