title: How does return work in try, catch, finally in Java?
body: <p>I can't understand exactly how <code>return</code> works in <code>try</code>, <code>catch</code>.</p>
  
  <ul>
  <li>If I have <code>try</code> and <code>finally</code> without <code>catch</code>, I can put <code>return</code> inside the <code>try</code> block.</li>
  <li>If I have <code>try</code>, <code>catch</code>, <code>finally</code>, I can't put <code>return</code> in the <code>try</code> block.</li>
  <li>If I have a <code>catch</code> block, I must put the <code>return</code> outside of the <code>try</code>, <code>catch</code>, <code>finally</code> blocks.</li>
  <li>If I delete the <code>catch</code> block and <code>throw Exception</code>, I can put the <code>return</code> inside the <code>try</code> block.</li>
  </ul>
  
  <p>How do they work exactly? Why I can't put the <code>return</code> in the <code>try</code> block?</p>
  
  <p>Code with <code>try</code>, <code>catch</code>, <code>finally</code></p>
  
  <pre><code> public int insertUser(UserBean user) {
       int status = 0;
  
       Connection myConn = null;
       PreparedStatement myStmt = null;
  
       try {
           // Get database connection
           myConn = dataSource.getConnection();
  
           // Create SQL query for insert
           String sql = "INSERT INTO user "
                      + "(user_name, name, password) "
                      + "VALUES (?, ?, ?)";
  
           myStmt = myConn.prepareStatement(sql);
  
           // Set the parameter 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></pre>
  
  <p>Code with <code>try</code>, <code>finally</code> without <code>catch</code></p>
  
  <pre><code> public int insertUser(UserBean user) throws Exception {
       int status = 0;
  
       Connection myConn = null;
       PreparedStatement myStmt = null;
  
       try {
           // Get database connection
           myConn = dataSource.getConnection();
  
           // Create SQL query for insert
           String sql = "INSERT INTO user "
                      + "(user_name, name, password) "
                      + "VALUES (?, ?, ?)";
  
           myStmt = myConn.prepareStatement(sql);
  
           // Set the parameter 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);
       }
   }
  </code></pre>