在Java编程中,如何处理异常方法?

本例展示了如何使用System类的System.err.println()方法处理异常方法。

package com.yiibai;

public class ExceptionMethods {
    public static void main(String[] args) {
        try {
            throw new Exception("My Exception");
        } catch (Exception e) {
            System.err.println("Caught Exception");
            System.err.println("getMessage():" + e.getMessage());
            System.err.println("getLocalizedMessage():" + e.getLocalizedMessage());
            System.err.println("toString():" + e);
            System.err.println("printStackTrace():");
            e.printStackTrace();
        }
    }
}
Java

上述代码示例将产生以下结果 -

Caught Exception
getMessage():My Exception
getLocalizedMessage():My Exception
toString():java.lang.Exception: My Exception
printStackTrace():
java.lang.Exception: My Exception
    at com.yiibai.ExceptionMethods.main(ExceptionMethods.java:6)

相关文章:

  • 2022-12-23
  • 2023-03-12
  • 2021-10-16
  • 2021-07-13
  • 2022-01-13
  • 2022-02-26
  • 2022-01-17
猜你喜欢
  • 2022-12-23
  • 2021-10-01
  • 2021-09-28
  • 2021-06-13
  • 2022-02-11
  • 2022-12-23
相关资源
相似解决方案