Home  >  Article  >  Java  >  Java Example - Exception Handling Method

Java Example - Exception Handling Method

黄舟
黄舟Original
2017-02-04 10:21:271158browse

The following example demonstrates the use of System.err.println() of the System class to display the exception handling method:

/*
 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();
   }
 }
 }

The output result of running the above code is:

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)

Above It is the content of Java Example - Exception Handling Method. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn