关于异常捕获异常写入文件
希望把数组越界的异常写入一个文件中,并加入产生异常的时间。我写的代码如下
package com.cn.shijiu;import java.io.FileNotFoundException;import java.io.FileWriter;import java.io.IOException;import java.io.PrintStream;import java.text.SimpleDateFormat;import java.util.Date;public class Test1{ public static void main(String args[]) throws IOException { try { int[] a = new int[2]; System.out.println(a[3]); } catch (Exception e) { try { FileWriter fw = new FileWriter("d:\\log.txt"); Date d = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String s = sdf.format(d); fw.write(s); fw.flush(); e.printStackTrace(new PrintStream("d:\\log.txt")); } catch (FileNotFoundException e1) { e1.printStackTrace(); } } }}import java.io.*;import java.text.SimpleDateFormat;import java.util.Date;public class Test1{ public static void main(String args[]) throws IOException { try { int[] a = new int[2]; System.out.println(a[3]); } catch (Exception e) { try { FileWriter fw = new FileWriter("d:\\log.txt",true); Date d = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String s = sdf.format(d); fw.write(s); fw.write("\r\n"); fw.flush(); e.printStackTrace(new PrintWriter(fw)); fw.close(); } catch (FileNotFoundException e1) { e1.printStackTrace(); } } }}