Java 实例 - 异常处理方法

黄舟
黄舟 原创
2017-02-04 10:21:27 939浏览

以下实例演示了使用 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 实例 - 异常处理方法的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!



声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。