How does return work in try, catch, finally in Java?

  • A+
Category:Languages

I can't understand exactly how return works in try, catch. I see that if I have try and finally without catch I can put return inside the try block.

And if I have try, catch, finally I can't put return in the try block. If I have catch block I must put the return outside of try, catch, finally blocks. If I delete the catch block and throws Exception I can put the return inside the try block. Can somedoby explain me how they work exactly? Why I can't put the return in the try block? Thanks in advance!

Code with try, catch, finally

 public int insertUser(UserBean user) {          int status = 0;          Connection myConn = null;         PreparedStatement myStmt = null;          try {              // get db connection             myConn = dataSource.getConnection();              // create sql for insert             String sql = "INSERT INTO user "                        + "(user_name, name, password) "                        + "VALUES (?, ?, ?)";              myStmt = myConn.prepareStatement(sql);              // set the param values for the student             myStmt.setString(1, user.getUsername());             myStmt.setString(2, user.getName());             myStmt.setString(3, user.getPassword());              // execute sql insert             myStmt.execute();           } catch (Exception exc) {              System.out.println(exc);         } finally {              // clean up JDBC objects             close(myConn, myStmt, null);         }          return status;  } 

Code with try, finally without catch

 public int insertUser(UserBean user) throws Exception {          int status = 0;          Connection myConn = null;         PreparedStatement myStmt = null;          try {              // get db connection             myConn = dataSource.getConnection();              // create sql for insert             String sql = "INSERT INTO user "                        + "(user_name, name, password) "                        + "VALUES (?, ?, ?)";              myStmt = myConn.prepareStatement(sql);              // set the param values for the student             myStmt.setString(1, user.getUsername());             myStmt.setString(2, user.getName());             myStmt.setString(3, user.getPassword());              // execute sql insert             myStmt.execute();              return status;         } finally {              // clean up JDBC objects             close(myConn, myStmt, null);         }   } 

 


Yes it's confusing.

In Java, all program control paths of a non-void function must finish with a return, or throw an exception. That's the rule put nice and simply.

But, in an an abomination, Java allows you to put an extra return in a finally block, which overrides any previously encountered return:

try {     return foo; // This is evaluated... } finally {     return bar; // ...and so is this one, and the previous `return` is discarded } 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: