开发者

syntax warning showing: exception is never thrown in body of try statement

开发者 https://www.devze.com 2023-04-01 12:41 出处:网络
public class SetupConnection{ public static Statement setCon(){ Connection con=null; Statement st=null; try{
public class SetupConnection{

  public static Statement setCon(){
    Connection con=null;
    Statement st=null;                

    try{
      Class.forName("com.mysql.jdbc.Driver").newInstanc开发者_开发问答e();
      con=DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/zomatocrm","root","");
      st = con.createStatement();
    }
    catch(ConnectException err){
      JOptionPane.showMessageDialog(null, "Connection refused!!!");
      System.out.println(err.getMessage());
    }            
    catch(Exception e){
      System.out.println(e.getMessage());                   
    }   
    return st;
  }

The warning:

exception is never thrown in body of try statement

is coming up at the catch(ConnectException){ line.


None of the code in your try block:

Class.forName("com.mysql.jdbc.Driver").newInstance();     
con=DriverManager.getConnection(
    "jdbc:mysql://localhost:3306/zomatocrm","root","");
st = con.createStatement();

...can ever throw a ConnectException. Therefore the compiler is warning you that this catch clause:

catch(ConnectException err){
    JOptionPane.showMessageDialog(null, "Connection refused!!!");
    System.out.println(err.getMessage());
} 

is redundant. If you remove it, the warning will go away.


getConnection and createStatement throw SQLException, which is caught in your second catch block. ConnectException is never thrown...


Note, if the connection to the database fails due to network problems, you'll still only get a SQLException, so catching that would be better and if you want to determine whether there was a networking problem you could check the SQLState (according to the manual):

try {
    ...
} catch (SQLException sqle) {
    if ("08S01".equals(sqle.getSQLState())) {
        JOptionPane.showMessageDialog(null, "Connection refused (or networking problem)!!!");
        System.out.println(err.getMessage());
    }
}

Also (as a general point, not related to this error), your method creates a connection (con object), but no reference to it is ever returned, so how are you closing the connection? Not closing a connection can lead to a connection leak, which will cause you problems.


Your real question has been answered by others. I just want to point out that this ...

catch (Exception e) {
    System.out.println(e.getMessage());                   
}   
return st;

... is really bad code.

What you are doing is dumping a stacktrace to stderr, and then continuing as if nothing bad has happened. The problem is:

  • the Exception you just caught could be caused by all sorts of things, some of which should not be ignored, and

  • the stderr stream might be connected to nothing,

  • (maybe worse) the raw stacktrace could be displayed to a non-Java literate user who won't understand it and who is likely to either ignore it or freak out!

0

精彩评论

暂无评论...
验证码 换一张
取 消