java Java中的错误类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5217712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error class in Java
提问by satheesh
I am trying to understand the Error
class in Java.
我试图理解Error
Java 中的类。
I have a good understanding of the Exception
class, but can't find examples of code for the Error
class. I've tried searching the web and also the java.sun website, but I'm not finding anything useful to help me understand this better.
我对这个Exception
类有很好的理解,但找不到Error
该类的代码示例。我试过搜索网络和 java.sun 网站,但我没有找到任何有用的东西来帮助我更好地理解这一点。
How do I use the Error
class in programs and where do we have to use that?
我如何Error
在程序中使用这个类,我们必须在哪里使用它?
回答by Joachim Sauer
You don't use Error
in your code.
你不在Error
你的代码中使用。
An Error
is a specific kind of Throwable
, just as Exception
is.
AnError
是 的一种特定类型Throwable
,原样Exception
。
Throwable
is the base class that defines everything that can be thrown.Exception
is the common case. It is about problems that occur during the execution of your program.RuntimeException
is a special case: it's unchecked (i.e. it need not be declared by a method and the compiler doesn't force you to catch it).
Error
is the "rare" case: it signifies problems that are outside the control of the usual application: JVM errors, out of memory, problems verifying bytecode: these are things that you should not handlebecause if they occur things are already so bad that your code is unlikely to be able to handle it sanely.
Throwable
是定义可以抛出的所有内容的基类。Exception
是常见的情况。它是关于程序执行过程中出现的问题。RuntimeException
是一种特殊情况:它是未经检查的(即它不需要由方法声明并且编译器不会强迫您捕获它)。
Error
是“罕见”的情况:它表示通常应用程序无法控制的问题:JVM 错误、内存不足、字节码验证问题:这些是您不应该处理的事情,因为如果它们发生,事情已经很糟糕了,以至于您的代码不太可能能够理智地处理它。
You should not attempt to correct the situation that resulted in an Error
. You might want to catch it in order to log it and then rethrow it (see the JavaDoc of ThreadDeath
for an example on why you need to rethrow it (thanks to @krock for the heads-up)).
您不应试图纠正导致Error
. 您可能想要捕获它以记录它然后重新抛出它(ThreadDeath
有关为什么需要重新抛出它的示例,请参阅 JavaDoc (感谢 @krock 的提示))。
There is no other reason to throw any Error
(i.e. don't create an Error
on your own and throw it, if you think you want to do that, use an Exception
or a RuntimeException
instead).
没有其他原因可以抛出任何Error
(即不要Error
自己创建一个并抛出它,如果您认为要这样做,请改用 anException
或 a RuntimeException
)。
回答by reef
If you take a look at the Javadoc herethere is a good explanation:
如果你看看这里的 Javadoc有一个很好的解释:
An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions.
Error 是 Throwable 的一个子类,它指示合理的应用程序不应尝试捕获的严重问题。大多数此类错误是异常情况。
Concerning the usage you also have this:
关于用法,你还有这个:
A method is not required to declare in its throws clause any subclasses of Error that might be thrown during the execution of the method but not caught, since these errors are abnormal conditions that should never occur.
一个方法不需要在它的 throws 子句中声明在方法执行期间可能抛出但没有被捕获的 Error 的任何子类,因为这些错误是不应该发生的异常情况。
回答by JAVA
Error
Error a subclass of "Throwable" class is thrown by java runtime system and indicate some unrecoverable conditions during the execution of the programs Once thrown difficult to recover from it and the application get to halt. Eg..,java.lang.StackOverflowError and java.lang.OutofMemoryError
错误
java运行时系统抛出“Throwable”类的子类错误,表明程序执行过程中出现了一些不可恢复的情况,一旦抛出很难恢复,应用程序就会停止。例如..,java.lang.StackOverflowError 和 java.lang.OutofMemoryError
// An example code which throws StackOverflowError
//抛出 StackOverflowError 的示例代码
public class ErrorDemo
{
public void method1()
{
this.method2();
}
public void method2()
{
this.method1();
}
public static void main(String sri[])
{
ErrorDemo k= new ErrorDemo();
k.method1();
}
}
In this code from main method we are calling method1 and from method1 a call was made to method2 and again from method2 we are calling method1 means we created a continous loop which doesn't goes to end and finally a critical error StackOverflowErroris being thrown.
在 main 方法的这段代码中,我们调用了 method1,从 method1 调用了 method2,而从 method2 调用了 method1,这意味着我们创建了一个不会结束的连续循环,最后抛出一个严重错误 StackOverflowError。