Home > Mobile >  Understanding what will happen in case i will encounter one of this exceptions in a project
Understanding what will happen in case i will encounter one of this exceptions in a project

Time:01-18

Hello I have to work at a project which I forked from github and I have some problems in understanding exactly what will happen when i will encounter of of the exceptions present in my code.

try {
    Class.forName("com.mysql.cj.jdbc.Driver");
    databaseLink=DriverManager.getConnection(url,databaseuser,databasepassword);
} catch (ClassNotFoundException e) {
    System.out.println("Error: Could not find the JDBC driver");
    Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, e);
    System.exit(0);
} catch (SQLException e) {
    System.out.println("Error: Could not establish a connection to the database");
    Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, e);
    System.exit(0);
}

I have never used Logger class before and in order to understand what will happen. In case on of this exceptions will be caught the message will be somehow redirected and the message printed will be the one from system.out and it will end with exit code 0? If this is the case where exactly Logger will redirect the message, because i didn't manage to understand even after looking on the java documentation from the Logger class

CodePudding user response:

Logger only write traces in file

Whit getLogger(this.getClass().getName()) They try identify where must write log trace, in this case where class name is configured (your project provably has properties configuration file or something like)

There indicates log tipe, text and, in this case, exception traze Level.SEVERE, null, e)

SEVERE (highest value) WARNING INFO CONFIG FINE FINER FINEST

SEVERE is a big error, I can't continue with the execution, Stop application (Logger can't stop, it just says it will stop now). For example I can load the DDBB to continue with the execution.

null is the string that you locate in log file to search the traze, null is bad idea. Is normal put something like "Error: Could not find the JDBC driver" and delete line with System.out.prinln.

And 'e' is the error, the log write error line and all class afected.

Logger only write. This line Stop the execution.

System.exit(0);
  • Related