java异常的三种打印⽅法_⽤Java打印异常消息的不同⽅法java异常的三种打印⽅法
various methods to provide exception related
Throwable class provides various methods to provide exception related Whenever exception throw then Throwable class
information like Exception name, Exception description and Stack Trace, etc.
information
各种⽅法来提供与异常相关的信息,例如异常名称,异常描述和堆栈跟踪等。
Throwable类都会提供各种⽅法来提供与异常相关的信息,
每当引发异常时, Throwable类都会
methods of Throwable class which provides exception related information so the name of these We will discuss three methods of Throwable class
methods are:
⽅法 ,这些⽅法提供了与异常相关的信息,因此这些⽅法的名称为:
我们将讨论Throwable类的
Throwable类的三种⽅法
1. printStackTrace() method
printStackTrace()⽅法
2. toString() method
toString()⽅法
3. getMessage() method
getMessage()⽅法
We will see what is the purpose of these methods and how
我们将看到这些⽅法的⽬的是什么以及它是如何⼯作的...
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.String());
} 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.String().
软件包java.String()中提供了此⽅法。
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.String());
}
}
}
Output
java的tostring方法输出量
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.Message());        }
}
}
Output
输出量
E:\Programs>javac GetMessageMethod.java
E:\Programs>java GetMessageMethod
/
by zero
java异常的三种打印⽅法

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。