title: In Java, how do I set a return type if an exception occurs?
body: <p>hey all, I'm new to Java and was wondering if I define a method to return a database object</p>
  
  <p>like </p>
  
  <pre><code>import java.sql.*;
  
  public class DbConn {
  
      public Connection getConn() {
          Connection conn;
          try {
              Class.forName("com.mysql.jdbc.Driver").newInstance();
              if(System.getenv("MY_ENVIRONMENT") == "development") {
                  String hostname = "localhost";
                  String username = "root";
                  String password = "root";
              }
              conn = DriverManager.getConnection("jdbc:mysql:///mydb", username, password);
              return conn;
          } catch(Exception e) {
              throw new Exception(e.getMessage());
          }
  
      }
  
  }
  </code></pre>
  
  <p>if the connection fails when I try to create it what should I return? eclipse is telling me I have to return a Connection object but if it fails I'm not sure what to do. </p>
  
  <p>thanks!</p>
  
  <p>UPDATED CODE TO LET EXCEPTION BUBBLE:</p>
  
  <pre><code>public class DbConn {
  
      public Connection getConn() throws SQLException {
          Connection conn;
          String hostname = "localhost";
          String username = "root";
          String password = "root";
  
          Class.forName("com.mysql.jdbc.Driver").newInstance();
          if(System.getenv("MY_ENVIRONMENT") != "development") {
              hostname = "localhost";
              username = "produser";
              password = "prodpass";
          }
          conn = DriverManager.getConnection("jdbc:mysql:///mydb", username, password);
          return conn;
  
      }
  
  }
  </code></pre>