广州北大青鸟计算机职业培训学校
互联网技术培训、软件技术培训、大数据培训、云计算培训、数据分析培训信息网
当前位置:网站首页 > 软件教程 > Java技术 > 正文

惠州什么是Throwable?_北大青鸟IT学校

作者:邓华发布时间:2021-04-04分类:Java技术浏览:780


导读:什么是Throwable?​相信这是每个学习Java的同学都会遇到的问题,下面我们一起来看看惠州北大青鸟老师是怎么回答的。

什么是Throwable?相信这是每个学习Java的同学都会遇到的问题,下面我们一起来看看惠州北大青鸟老师是怎么回答的。

下面的列表显示了Throwable类的一些常用方法。

Throwable类是Java中所有异常类的超类。此表中显示的所有方法在所有异常类中都可用。

  1. Throwable getCause()
    返回异常的原因。如果未设置异常的原因,则返回null。

  2. String getMessage()
    返回异常的详细消息。

  3. StackTraceElement[] getStackTrace()
    返回堆栈跟踪元素的数组。

  4. Throwable initCause(Throwable cause)
    设置异常的原因。
    有两种方法可以将异常设置为异常的原因。其他方法是使用构造函数,它接受原因作为参数。

  5. void printStackTrace()
    在标准错误流上打印堆栈跟踪。

  6. void printStackTrace(PrintStream s)
    将堆栈跟踪打印到指定的PrintStream对象。

  7. void printStackTrace(PrintWriter s)
    将堆栈跟踪打印到指定的PrintWriter对象。

  8. String toString()
    返回异常对象的简短描述。

实例1

以下代码演示了对异常类使用printStackTrace()方法。

public class Main {  public static void main(String[] args) {    try {
     m1();
   } catch (MyException e) {
     e.printStackTrace(); // Print the stack trace    }
 }  public static void m1() throws MyException {
   m2();
 }  public static void m2() throws MyException {    throw new MyException("Some  error has  occurred.");
 }
}class MyException extends Exception {  public MyException() {    super();
 }  public MyException(String message) {    super(message);
 }  public MyException(String message, Throwable cause) {    super(message, cause);
 }  public MyException(Throwable cause) {    super(cause);
 }
}

上面的代码生成以下结果。

MyException:Some   error has  occurred.

at Main.m2(Main.java:16)

at Main.m1(Main.java:12)

at Main.main(Main.java:5)

实例2

以下代码显示了如何将异常的堆栈跟踪写入字符串。

import java.io.PrintWriter;import java.io.StringWriter;public class Main {  public static void main(String[] args) {    try {
     m1();
   } catch (MyException e) {
     String str = getStackTrace(e);
     System.out.println(str);
   }
 }  public static void m1() throws MyException {
   m2();
 }  public static void m2() throws MyException {    throw new MyException("Some  error has  occurred.");
 }  public static String getStackTrace(Throwable e) {
   StringWriter strWriter = new StringWriter();
   PrintWriter printWriter = new PrintWriter(strWriter);
   e.printStackTrace(printWriter);    // Get the stack trace as a string    String str = strWriter.toString();    return str;
 }
}class MyException extends Exception {  public MyException() {    super();
 }  public MyException(String message) {    super(message);
 }  public MyException(String message, Throwable cause) {    super(message, cause);
 }  public MyException(Throwable cause) {    super(cause);
 }
}

上面的代码生成以下结果。

MyException:Some   error has  occurred.

at Main.m2(Main.java:19)

at Main.m1(Main.java:15)

at Main.main(Main.java:8)

通过老师的讲解,你知道什么是Throwable了吗?想了解更多关于Java的知识,联系在线客服,或者来惠州北大青鸟新方舟校区了解一下。

java8.png

Java

标签:惠州计算机JAVA软件开发惠州计算机Java软件开发惠州计算机JAVA培训惠州计算机JAVA软件开发学校惠州计算机Java软件开发培训JAVAJava软件开发北大青鸟IT计算机学校北大青鸟IT软件学校北大青鸟IT学校


Java技术排行
标签列表
网站分类
文章归档
最近发表