-1

Can somebody please tell me how to fix the error on line 8 of my Test class I have no idea wtf is wrong with it or how to even search for solution

Main class

package T;    
public class Test {        
    public static void main(String[] args) {                    
        SQL sql = new SQL();
        sql.getData();//Error here "Unreported exception Exception; must be caught or declared to be thrown" 
    }
}

SQL Class

package T;
public class SQL {
    private Connection connect = null;   

    public String getData() throws Exception {
        String name = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connect = DriverManager.getConnection("jdbc:mysql://localhost/vehicles?" + "user=sqluser&password=sqluserpw");
            Statement st = connect.createStatement();   
            ResultSet rs = st.executeQuery("SELECT * from vehicles.test");                    
            if (rs.next()) {
               name = rs.getString("word");
            } 
            return name;
        } 
        catch (Exception e) {
            throw e;
        }
    }
}
Minar Mahmud
  • 2,577
  • 6
  • 20
  • 32
Faktuu
  • 1
  • 1
  • Seems like you should also have a `finally` block (following the catch block) that closes the resultset and closes the statement. And close the connection as well, since you've gotten the connection in the try block. (It's possible that `rs` and `st` could be null, so do a check for null before calling the close method.) – spencer7593 Apr 30 '15 at 13:53
  • Why you are catching exception in getData() if you are throwing it again in catch block? Catch it and dont throw or dont catch and add thorws to getData and Catch in main. – Garry Apr 30 '15 at 13:57
  • The signature of getdata() shows that it can throw an exception. So, at the place you call it, you need to catch that exception OR the signature of that calling method must allow the exception to be thrown further up the call stack. Since you're calling it from main, you must catch the exception as long as you keep the signature of getData() the way it is now. – Darius X. Apr 30 '15 at 14:09

5 Answers5

1

The method in your SQL class throws an exception and in the main method you don't handle that exception. This is a checked exception so you MUST do something about it. At least you need to have a simple catch like that

public static void main(String[] args) {

    SQL sql = new SQL();
    try{
      sql.getData();//Error here "Unreported exception Exception; must be caught or declared to be thrown" 
    }catch(Exception e){
      e.printStackTrace();
    }
}
Veselin Davidov
  • 7,031
  • 1
  • 15
  • 23
0

As your Calling method is throwing the exception it is required to catch that exception in Called method.So just put your call inside try catch block.

0

is that questionmark necessary? is not a typo and you meant something else? here are some examples of sqlconnections in java, they look a little bit different than yours. How to make SQL connection statements common

Community
  • 1
  • 1
0

Simply surround your line with a try-catch loop

Blip
  • 3,061
  • 5
  • 22
  • 50
0
public String getData() throws Exception {
    //
}

Here throws Exception means that this method might throw an exception of type Exception, which is a checked exception.

So it must be handled while calling this method. Means use a try/catch.

This article might help you learn about checked and unchecked exceptions.

Minar Mahmud
  • 2,577
  • 6
  • 20
  • 32