Home > Java > javaTutorial > body text

Java Example - Exception Handling Method

黄舟
Release: 2017-02-04 10:21:27
Original
1243 people have browsed it

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();
   }
 }
 }
Copy after login

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)
Copy after login

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



Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!