Java 中的 printStackTrace() 方法有什么用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2560368/
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
What is the use of printStackTrace() method in Java?
提问by Venkat
I am going through a socket program. In it, printStackTrace
is called on the IOException
object in the catch block.
What does printStackTrace()
actually do?
我正在通过一个套接字程序。在其中,在 catch 块中printStackTrace
的IOException
对象上调用。
什么是printStackTrace()
真正做到?
catch(IOException ioe)
{
ioe.printStackTrace();
}
I am unaware of its purpose. What is it used for?
我不知道它的目的。它是干什么用的?
采纳答案by Joachim Sauer
It's a method on Exception
instances that prints the stack trace of the instance to System.err
.
它是Exception
实例上的一种方法,可将实例的堆栈跟踪打印到System.err
.
It's a very simple, but very useful tool for diagnosing an exceptions. It tells you what happened and where in the code this happened.
这是一个非常简单但非常有用的诊断异常的工具。它会告诉您发生了什么以及在代码中发生的位置。
Here's an example of how it might be used in practice:
下面是一个如何在实践中使用它的例子:
try {
// ...
} catch (SomeException e) {
e.printStackTrace();
}
回答by yogesh pathak
It helps to trace the exception. For example you are writing some methods in your program and one of your methods causes bug. Then printstack will help you to identify which method causes the bug. Stack will help like this:
它有助于跟踪异常。例如,您正在程序中编写一些方法,而其中一种方法会导致错误。然后 printstack 将帮助您确定导致错误的方法。堆栈会像这样提供帮助:
First your main method will be called and inserted to stack, then the second method will be called and inserted to the stack in LIFO order and if any error occurs somewhere inside any method then this stack will help to identify that method.
首先你的 main 方法将被调用并插入到堆栈中,然后第二个方法将被调用并以 LIFO 顺序插入到堆栈中,如果任何方法内部发生任何错误,那么这个堆栈将有助于识别该方法。
回答by Jens Bodal
I was kind of curious about this too, so I just put together a little sample code where you can see what it is doing:
我也对这个很好奇,所以我把一些示例代码放在一起,你可以看到它在做什么:
try {
throw new NullPointerException();
}
catch (NullPointerException e) {
System.out.println(e);
}
try {
throw new IOException();
}
catch (IOException e) {
e.printStackTrace();
}
System.exit(0);
Calling println(e)
:
呼叫println(e)
:
java.lang.NullPointerException
java.lang.NullPointerException
Calling e.printStackTrace()
:
呼叫e.printStackTrace()
:
java.io.IOException at package.Test.main(Test.java:74)
java.io.IOException at package.Test.main(Test.java:74)
回答by TheArchon
printStackTrace()
prints the locations where the exception occurred in the source code, thus allowing the author who wrote the program to see what went wrong. But since it shows problems in the source code, the user(s) who may or may not have any coding experience may not be able to understand what went wrong, so if the program allows the user to send error messages to the authors, the users may not be able to give good data on what went wrong.
printStackTrace()
打印源代码中发生异常的位置,从而让编写程序的作者看到出了什么问题。但是由于它显示了源代码中的问题,可能有或可能没有任何编码经验的用户可能无法理解哪里出了问题,所以如果程序允许用户向作者发送错误消息,用户可能无法提供有关出错的好数据。
You should consider the Logger.getLogger()
method, it offers a better exception handling (logging) facility, and besides printStackTrace()
without arguments is considered to be obsolete and should ONLY be used for debugging purposes, not for user display.
您应该考虑该Logger.getLogger()
方法,它提供了更好的异常处理(日志记录)工具,此外printStackTrace()
没有参数被认为是过时的,应该仅用于调试目的,而不是用于用户显示。
回答by Ashish Gope
printStackTrace()
helps the programmer to understand where the actual problem occurred. printStacktrace()
is a method of the class Throwable
of java.lang
package. It prints several lines in the output console.
The first line consists of several strings. It contains the name of the Throwable sub-class & the package information.
From second line onwards, it describes the error position/line number beginning with at
.
printStackTrace()
帮助程序员了解实际问题发生的位置。printStacktrace()
是包类Throwable
的方法java.lang
。它在输出控制台中打印几行。第一行由几个字符串组成。它包含 Throwable 子类的名称和包信息。从第二行开始,它描述了以 开头的错误位置/行号at
。
The last line always describes the destination affected by the error/exception. The second last line informs us about the next line in the stack where the control goes after getting transfer from the line number described in the last line. The errors/exceptions represents the output in the form a stack, which were fed into the stack by fillInStackTrace()
method of Throwable
class, which itself fills in the program control transfer details into the execution stack. The lines starting with at
, are nothing but the values of the execution stack.
In this way the programmer can understand where in code the actual problem is.
最后一行总是描述受错误/异常影响的目的地。倒数第二行告诉我们堆栈中的下一行,在从最后一行中描述的行号获取传输后,控制将转到哪里。错误/异常表示以堆栈形式的输出,这些输出通过类的fillInStackTrace()
方法送入堆栈,Throwable
类本身将程序控制传输细节填充到执行堆栈中。以at
,开头的行只不过是执行堆栈的值。通过这种方式,程序员可以了解实际问题在代码中的哪个位置。
Along with the printStackTrace()
method, it's a good idea to use e.getmessage()
.
除了printStackTrace()
方法之外,最好使用e.getmessage()
.
回答by Ashish Gope
What is the use of e.printStackTrace() method in Java?
Java 中的 e.printStackTrace() 方法有什么用?
Well, the purpose of using this method e.printStackTrace();
is to see what exactly wrong is.
嗯,使用这个方法的目的e.printStackTrace();
就是看看到底哪里错了。
For example, we want to handle an exception. Let's have a look at the following Example.
例如,我们要处理异常。让我们看看下面的例子。
public class Main{
public static void main(String[] args) {
int a = 12;
int b = 2;
try {
int result = a / (b - 2);
System.out.println(result);
}
catch (Exception e)
{
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
}
I've used method e.printStackTrace();
in order to show exactly what is wrong.
我已经使用了方法e.printStackTrace();
来准确显示出了什么问题。
In the output, we can see the following result.
在输出中,我们可以看到以下结果。
Error: / by zero
java.lang.ArithmeticException: / by zero
at Main.main(Main.java:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
回答by Rasmi Ranja Choudhury
The printStackTrace()
helps the programmer understand where the actual problem occurred. The printStackTrace()
method is a member of the class Throwable
in the java.lang
package.
这printStackTrace()
有助于程序员了解实际问题发生的位置。该printStackTrace()
方法是类的一个成员Throwable
在java.lang
包。
回答by Bablu Mgc
printStackTrace
is a method of the Throwable
class. This method displays error message in the console; where we are getting the exception in the source code. These methods can be used with catch block and they describe:
printStackTrace
是Throwable
类的方法。此方法在控制台中显示错误消息;我们在源代码中得到异常的地方。这些方法可以与 catch 块一起使用,它们描述了:
- Name of the exception.
- Description of the exception.
- Location of the exception in the source code.
- 异常的名称。
- 异常的描述。
- 异常在源代码中的位置。
The three methods which describe the exception on the console (in which printStackTrace is one of them) are:
在控制台上描述异常的三个方法(其中 printStackTrace 是其中之一)是:
printStackTrace()
toString()
getMessage()
printStackTrace()
toString()
getMessage()
Example:
例子:
public class BabluGope {
public static void main(String[] args) {
try {
System.out.println(10/0);
} catch (ArithmeticException e) {
e.printStackTrace();
// System.err.println(e.toString());
//System.err.println(e.getMessage());
}
}
}
回答by RohanMNirer
printStackTrace() helps the programmer to understand where the actual problem occurred. It helps to trace the exception. it is printStackTrace() method of Throwable class inherited by every exception class. This method prints the same message of e object and also the line number where the exception occurred.
printStackTrace() 帮助程序员了解实际问题发生的位置。它有助于跟踪异常。它是由每个异常类继承的 Throwable 类的 printStackTrace() 方法。此方法打印 e 对象的相同消息以及发生异常的行号。
The following is an another example of print stack of the Exception in Java.
下面是Java中异常打印堆栈的另一个例子。
public class Demo {
public static void main(String[] args) {
try {
ExceptionFunc();
} catch(Throwable e) {
e.printStackTrace();
}
}
public static void ExceptionFunc() throws Throwable {
Throwable t = new Throwable("This is new Exception in Java...");
StackTraceElement[] trace = new StackTraceElement[] {
new StackTraceElement("ClassName","methodName","fileName",5)
};
t.setStackTrace(trace);
throw t;
}
}
java.lang.Throwable: This is new Exception in Java... at ClassName.methodName(fileName:5)
java.lang.Throwable:这是 Java 中的新异常...在 ClassName.methodName(fileName:5)
回答by S S Tiwari
printStackTrace()
tells at what line you are getting error any why are you getting error.
printStackTrace()
告诉您在哪一行出现错误,以及为什么会出现错误。
Example:
例子:
java.lang.ArithmeticException: / by zero
at MinNumber.main(MinNumber.java:8)