致命错误VS异常

时间:2016-03-30 12:23:46

标签: java exception runtime-error

我知道错误 [运行时错误]和例外之间的区别。{谈论JAVA }。 但是致命错误和异常是相同的事情还是它们是不同的。或者java中没有致命错误的概念? 我搜索谷歌,但它很混乱。

2 个答案:

答案 0 :(得分:6)

阅读Java tutorial on Exceptions

简短说明您可以使用这个小指南。请参阅每个类别中带有Java异常层次结构(红色)和几个示例(蓝色)的类图。

enter image description here

Throwable :使用Java异常机制抛出(和捕获)的任何东西的共同祖先。

Error :您可能会考虑Error关于您所说的内容"致命异常"。即使尝试恢复应用程序通常也没有意义。 OutOfMemoryErrorStackOverflowError是致命错误,您无法在应用程序中执行任何操作。

不要抓住Error,让程序崩溃并解决外面的问题。

RuntimeException :应该主要用于程序员的错误(请参阅下面的示例),例如未处理的空指针de-参考,不履行方法合同等。

虽然它们是将所有异常包装到RuntimeException并在业务代码之外处理(通常使用AOP)的技术,但通常出现RuntimeException意味着您应该让程序崩溃并修复其源代码。

/**
 * Does something with s.
 * @param s The input string
 */
public void badlyDesignedMethod(String s) {
    // Here the programmer omitted to check s against null
    int i = s.length();
    ...
}

/**
 * Does something with s.
 * @param s The input string; must not be null
 */
public void betterDesignedMethod(String s) {
    if (s == null) {
        throw new IllegalArgumentException("The parameter s must not be null!);
    }
    int i = s.length();
    ...
}

public void caller() {
    badlyDesignedMethod(null); // will throw NullPointerException
    // It is the fault of the programmer of badlyDesignedMethod()
    // as they have not specified that the parameter must not be null.

    betterDesignedMethod(null); // will throw IllegalArgumentException
    // Now it is the fault of the programmer of caller()
    // as they violated the contract of betterDesignedMethod()
    // which DOES tell them that the parameter must not be null
}

ErrorRuntimeException取消选中,这意味着方法不必声明它会抛出它们而调用者不必捕获它们,即没有用try-catch块围绕呼叫。

其他例外(继承自Exception来自RuntimeException):应该声明的例外情况( throws...)和捕获try-catch),它们主要用于从方法向其调用者传递有用信息:数据库不可用,文件无法打开等。

这些是您的代码要处理的异常。它们是预期的异常",与运行时异常相比,这些异常是"意外异常"。

public void doSomethingWithFile(String fileName) throws IOException {
    if (/* file is not accessible */) {
        throw new IOException("File " + fileName + " is not accessible!");
    }
    ...
}
public void caller() {
    try {
        doSomethingWithFile("/home/honza/file.txt");
    }
    catch (IOException e) {
        // handle the fact that the file is not accessible
        return;
    }
    ...
}

答案 1 :(得分:1)

单词FATAL通常用于记录器框架的上下文中,例如log4j,SLF4J,logback等等。现在,在查看日志文件时,可以找到

之类的消息
2015-09-28 15:21:48,222 Thread-4 FATAL Unable to register shutdown hook because JVM is shutting down. java.lang.IllegalStateException: Cannot add new shutdown hook as this is not started. Current state: STOPPED
at org.apache.logging.log4j.core.util.DefaultShutdownCallbackRegistry.addShutdownCallback(DefaultShutdownCallbackRegistry.java:113)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.addShutdownCallback(Log4jContextFactory.java:271)
at org.apache.logging.log4j.core.LoggerContext.setUpShutdownHook(LoggerContext.java:240)     

这表明操作导致发生错误或异常,并且开发人员使用FATAL级别的记录器框架记录了错误或异常。该消息可能已在任何级别记录。

Log4J例如提供了许多日志级别:OFF,FATAL,ERROR,WARN,INFO,DEBUG,TRACE,ALL

http://logging.apache.org/log4j/2.x/log4j-api/apidocs/org/apache/logging/log4j/Level.html

只是为了引起更多的混淆,有一个名为ERROR的日志级别。

总结一下,FATAL异常与FATAL错误: FATAL是记录异常或错误对象的日志级别,与发生的异常或错误无关,但是当开发人员在FATAL级别登录时,它会引起您的注意并引起反应。

根据Log4J的文档,关于何时登录致命等级 "严重错误,将阻止应用程序继续。"

相关问题