java异常的三种打印方法
Whenever exception throw then Throwable class provides various methods to provide exception related information like Exception name, Exception description and Stack Trace, etc.
每当引发异常时, Throwable类都会提供各种方法来提供与异常相关的信息,例如异常名称,异常描述和堆栈跟踪等。
We will discuss three methods of Throwable class which provides exception related information so the name of these methods are:
我们将讨论Throwable类的三种方法 ,这些方法提供了与异常相关的信息,因此这些方法的名称为:
printStackTrace() method
printStackTrace()方法
toString() method
toString()方法
getMessage() method
getMessage()方法
We will see what is the purpose of these methods and how it works...
我们将看到这些方法的目的是什么以及它是如何工作的...
1)printStackTrace()方法 (1) printStackTrace() method)
This method is available in the package java.lang.Throwable.printStackTrace().
软件包java.lang.Throwable.printStackTrace()中提供了此方法。
This method provides exception related information and we will see which information this method will provide.
此方法提供了与异常相关的信息,我们将看到此方法将提供哪些信息。
- Name of the Exception
- Description of the Exception
- Stack Trace of the Exception
Syntax:
句法:
Name of the Exception : Description of the Exception
Stack Trace of the Exception
Example:
例:
class PrintStackTrace {
public static void main(String[] args) {
Object obj = null;
try {
System.out.println(obj.toString());
} catch (Exception ex) {
/*Display exception name : exception description
Stack trace */
ex.printStackTrace();
}
}
}
Output
输出量
E:\Programs>javac PrintStackTrace.java
E:\Programs>java PrintStackTrace
java.lang.NullPointerException
at PrintStackTrace.main(PrintStackTrace.java:8)
2)toString()方法 (2) toString() method)
This method is available in the package java.lang.Throwable.toString().
软件包java.lang.Throwable.toString()中提供了此方法。
This method also provides exception related information and we will see again which information this method will provide.
此方法还提供了与异常相关的信息,我们将再次看到此方法将提供的信息。
- Name of the Exception
- Description of the Exception
Syntax:
句法:
Name of the Exception : Description of the Exception
Example:
例:
class ToStringMethod {
public static void main(String[] args) {
try {
int i = 10 / 0;
System.out.println(i);
} catch (Exception ex) {
// Display exception name : exception description
System.out.println(ex.toString());
}
}
}
Output
输出量
E:\Programs>javac ToStringMethod.java
E:\Programs>java ToStringMethod
java.lang.ArithmeticException: / by zero
3)getMessage()方法 (3) getMessage() method)
This method is also available in the package java.lang.Throwable.printStackTrace().
包java.lang.Throwable.printStackTrace()中也提供此方法。
This method provides exception related information and we will see which information this method will provide.
此方法提供了与异常相关的信息,我们将看到此方法将提供哪些信息。
Description of the Exception
异常说明
This method does not provide other information like exception name and exception stack trace.
此方法不提供其他信息,例如异常名称和异常堆栈跟踪。
Syntax:
句法:
Description of the Exception
Example:
例:
class GetMessageMethod {
public static void main(String[] args) {
try {
int i = 10 / 0;
System.out.println(i);
} catch (Exception ex) {
// Display exception description
System.out.println(ex.getMessage());
}
}
}
Output
输出量
E:\Programs>javac GetMessageMethod.java
E:\Programs>java GetMessageMethod
/ by zero
翻译自: https://www.includehelp.com/java/different-ways-to-print-exception-message-in-java.aspx
java异常的三种打印方法