java异常的三种打印方法_用Java打印异常消息的不同方法

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类的三种方法 ,这些方法提供了与异常相关的信息,因此这些方法的名称为:

  1. printStackTrace() method

    printStackTrace()方法

  2. toString() method

    toString()方法

  3. 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异常的三种打印方法

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值