/** Test **/
@Test
public void Test () throws exception{
String msg = "Success";
try{
int[] is = null;
System.out.println (is[10]);
}catch (Exception e) {
E.printstacktrace ();
Simpletoof.setrecord (Simpletoof.getexceptionmsg (e));
msg = "Fail";
}finally{
SYSTEM.OUT.PRINTLN (msg);
}
}
Simpletoof.class
/** Record Operation History **/
public static void Setrecord (String record) {
String Recordfilepath = "D://record/record" +gettypedate ("Yyyy-mm-dd", New Date ()) + ". txt";
System.out.println (Recordfilepath);
File filename = new file (Recordfilepath);
try{
if (!filename.exists ())
Filename.createnewfile ();
FileWriter FileWriter = new FileWriter (Recordfilepath, true); True represents an Append
Filewriter.write (Gettypedate ("YyyyMMdd HH:mm:ss", New Date ()) + "\ r \ n" +record+ "\ r \ n");
Filewriter.flush ();
Filewriter.close ();
}catch (Exception ex) {
Ex.printstacktrace ();
}
}
/** Get exception information **/
public static String getexceptionmsg (Exception e) {
StringBuffer emsg = new StringBuffer ();
if (e!=null) {
stacktraceelement[] st = E.getstacktrace ();
for (Stacktraceelement stacktraceelement:st) {
String Exclass = Stacktraceelement.getclassname ();
String method = Stacktraceelement.getmethodname ();
Emsg.append ("[Class: + Exclass +"] Call "+ Method +" When in the first "+ Stacktraceelement.getlinenumber ()
+ "Line code place exception!" Exception type: "+ e.tostring () +" \ r \ n ");
}
}
return emsg.tostring ();
}