3

I'm starting learning java (after many years of PHP) and I have some hard time trying to understand this java behavior.

I'm using jdk-8u121-macosx-x64 with mysql-connector-java-5.1.40-bin.jar registered in libraries property of my project in Intellij Idea 2016.3 CE.

If i try to connect as the picture below, i got a build error: connection-error

Bu if i put the same code inside a try catch block it works ok and i can query database with no problems: enter image description here

It is really necessary to put every query inside a try catch block? Is this an intended method by java or i'm doing something wrong?

Thanks

RDev
  • 1,221
  • 1
  • 12
  • 17
  • can you include your error here instead to an image, i can't see the image – Youcef LAIDANI Feb 05 '17 at 08:11
  • @YCF_L sure: Error:(15, 58) java: unreported exception java.sql.SQLException; must be caught or declared to be thrown – RDev Feb 05 '17 at 08:13
  • 3
    Read the Java Tutorial about checked exceptions. Just like the method signature requires arguments and return values to have certain types, it's `throws` list of exceptions requires handling exceptions of certain types. That way the compiler reminds you to take care of certain conditions. – Lew Bloch Feb 05 '17 at 08:21
  • @LewBloch thank you, as beginner I didn't know about checked and unchecked exceptions. Found this good article: [link](http://beginnersbook.com/2013/04/java-checked-unchecked-exceptions-with-examples/) that helped me fully understand this difference. Now it all make sense! – RDev Feb 05 '17 at 08:30
  • @RDev don't forgot to accept an answer – Youcef LAIDANI Feb 05 '17 at 10:22

2 Answers2

4

Generally we use try{}catch(){} if we are not sure if our code will return an error or not so for example this code here :

try {
    Class.forName("com.mysql.jdbc.Driver");
    java.sql.Connection con = DriverManager.getConnection(DB_URL, DB_username, DB_password);
    return con;

} catch (ClassNotFoundException | SQLException e) {
    System.out.println("Exception " + e);
    return null;
}

Consider you have a problem in your JDBC connector or in your SQL query so we catch the error inside the catch

}(ClassNotFoundException | SQLException e) {
    //catch error
}

If you don't want to use try catch you can throw it like this :

public Connection getConnection() throws ClassNotFoundException, SQLException {
    //------------------------------------------------^------------^--
    Class.forName(driver);
    java.sql.Connection con = DriverManager.getConnection(DB_URL, DB_username, DB_password);
    return con;

}

Hope this give you an idea.

You can learn more about this here :

Catching and Handling Exceptions

How should I use try-with-resources with JDBC?

Anatoly Shamov
  • 2,608
  • 1
  • 17
  • 27
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
2

Over the red lined part there is a balloon saying Unhandled Exception For solving this you can press Alt+Enter and add the throws expression into your main method signature or throw it by using a try/catch section.

The method which you had called it(DriverManager.getConnection(...)) has a throws in its signature and so you must be able to throw it via your method:

public static Connection getConnection(String url,
                   Properties info)
                            throws SQLException

You can find such code descriptions on java-docs like this https://docs.oracle.com/javase/7/docs/api/java/sql/DriverManager.html

Amir Keshavarz
  • 2,403
  • 3
  • 19
  • 19