Java itself defines the exception, logging simple description! Keep it, and then copy it.

Source: Internet
Author: User

LOG4J Related Preparation instructions: http://blog.csdn.net/liangrui1988/article/details/17435139

Define your own exceptions

Package Org.rui.exceptiontest;public class Extrafeature {//-------use------public static void F () throws myexception{ System.out.println ("MyException from F ()"); throw new MyException ();} public static void L () throws myexception{System.out.println ("MyException from L ()"); throw new MyException ("originated I n L () ");} public static void R () throws myexception{System.out.println ("MyException from R ()"), throw new MyException ("originated ( Origin) in R () ");} -------main---------public static void Main (string[] args) {try {f ()} catch (MyException e) {e.printstacktrace (System . out);} try {L ();} catch (MyException e) {e.printstacktrace (system.err);} try {R ();} catch (MyException e) {e.printstacktrace (System.out);    System.out.println ("Getlocalizedmessage:" +e.getlocalizedmessage ()); Stack trajectory for (stacktraceelement ste:e.getstacktrace ()) System.out.println ("MethodName:" +ste.getmethodname ());}}} Define your own exception---class MyException extends exception{private int x;public myexception () {}public myexception (String msg) {suPer (msg);} Public MyException (String Msg,int x) {super (msg); this.x=x;} public int val () {return x;} Public String Getmessge () {return "Detail Message:" +x+ "super.getmessage ()";}}

Exceptions and logs simple description

Package Org.rui.exceptiontest;import Java.io.printwriter;import Java.io.stringwriter;import Java.util.logging.Logger;  public class loggingexceptions{public static void Main (string[] args) {try {throw new loggingexception ();} catch (Logging Exception e) {System.err.print ("Caught:" +e);} Try {throw new loggingexception ();} catch (Loggingexception e) {System.err.print ("Caught2:" +e);}}}  Class Loggingexception extends Exception{private static Logger Logger=logger.getlogger ("loggingexception");p ublic Loggingexception () {StringWriter trace=new StringWriter ();p rintstacktrace (new PrintWriter (trace)); Logger.severe (" Severett: "+trace.tostring ());}}

Package Org.rui.exceptiontest;import Java.io.filenotfoundexception;import Java.io.printwriter;import Java.io.stringwriter;import Java.util.logging.Logger;  public class loggingexception2{private static Logger Logger=logger.getlogger ("loggingexception");  static void Logexception (Exception e) {StringWriter trace=new stringwriter (); E.printstacktrace (new PrintWriter (trace) ); Logger.severe ("Severett:" +trace.tostring ());}  


Java itself defines the exception, logging simple description! Keep it, and then copy it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.