您的位置:首页 > 编程语言 > Java开发

java IO流(异常的打印信息)

2017-03-21 19:58 274 查看
package quickstart;

import java.io.IOException;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* Created by patkritLee on 2017/3/21.
*/
public class ExceptionInfo {
public static void main(String[] agrs) throws IOException{
try{
int[] arr = new int[2];
System.out.println(arr[3]);
}catch(Exception e){
try{
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s = sdf.format(d);

PrintStream ps = new PrintStream("exception.log");
ps.println(s);
//                ps.write(d.toString().getBytes());
System.setOut(ps);
}
catch (IOException e1){
throw new RuntimeException("日志文件创建失败");
}
e.printStackTrace(System.out);
}
}
}
2017-03-21 19:56:23
java.lang.ArrayIndexOutOfBoundsException: 3
at quickstart.ExceptionInfo.main(ExceptionInfo.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: