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.