简体   繁体   English

了解如果我在项目中遇到其中一个异常会发生什么

[英]Understanding what will happen in case i will encounter one of this exceptions in a project

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.你好,我必须在一个我从 github 分叉的项目中工作,我在理解当我遇到我的代码中存在的异常时会发生什么时遇到了一些问题。

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.我以前从未使用过 Logger class,以便了解会发生什么。 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?如果捕获到此异常,消息将以某种方式重定向,打印的消息将是来自 system.out 的消息,并且将以退出代码 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如果是这种情况,Logger 将重定向消息,因为即使在查看 Logger class 的 java 文档后我也无法理解

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) Whit getLogger(this.getClass().getName()) 他们尝试确定必须在哪里写入日志跟踪,在这种情况下配置了 class 名称(您的项目可证明具有属性配置文件或类似文件)

There indicates log tipe, text and, in this case, exception traze Level.SEVERE, null, e)指示日志类型、文本,在本例中,指示异常 traze Level.SEVERE,null,e)

SEVERE (highest value) WARNING INFO CONFIG FINE FINER FINEST SEVERE(最高值)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). SEVERE 是一个大错误,我无法继续执行,Stop application(Logger 无法停止,它只是说它现在会停止)。 For example I can load the DDBB to continue with the execution.例如,我可以加载 DDBB 以继续执行。

null is the string that you locate in log file to search the traze, null is bad idea. null 是您在日志文件中找到的用于搜索 traze 的字符串,null 是个坏主意。 Is normal put something like "Error: Could not find the JDBC driver" and delete line with System.out.prinln.正常情况下会输入类似“错误:找不到 JDBC 驱动程序”的内容,并删除带有 System.out.prinln 的行。

And 'e' is the error, the log write error line and all class afected. 'e' 是错误,日志写入错误行和所有受影响的 class。

Logger only write.记录器只写。 This line Stop the execution.此行停止执行。

System.exit(0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM