Java 实例 – 异常处理方法
Java 实例
以下实例演示了使用 System 类的 System.err.println() 来展示异常的处理方法:
/*
author by w3cschool.cc
ExceptionDemo.java
*/
class ExceptionDemo
{
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();
}
}
}
以上代码运行输出结果为:
Caught Exception getMessage():My Exception getLocalizedMessage():My Exception toString():java.lang.Exception: My Exception printStackTrace(): java.lang.Exception: My Exception at ExceptionDemo.main(ExceptionDemo.java:5)
Java 实例

国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码













