异常处理机制--将异常写入指定文件中

PrintWrite 


// 将此 throwable 及其追踪输出到指定的 PrintWriter
/*
异常处理将异常追踪输出到一个指定文件夹里面
//File(URI uri) 通过将给定的 file: URI 转换为一个抽象路径名来创建一个新的 File 实例.
      
File file=new File(URL); //里面是一个正确地路径    

//PrintWriter(File file)   //里面是一个文件路径

PrintWrite pw=new PrintWrite(file); //会抛出FileNotFoundException

e.printStackTrace(pw); //异常e调用printStackTrace将异常写入文件中
*/


一个小案例



/**
package Exception.net;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;


public class ExceptionDome {
public static void main(String[] args) throws FileNotFoundException {
//将此 throwable 及其追踪输出到指定的 PrintWriter
File file = new File("E:\\workspace-2014\\20140924\\src\\Exception\\net\\a.txt");
PrintWriter pw = new PrintWriter(file);

int a[] = { 1, 2, 3 };
try {
System.out.println(a[4]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e.getMessage());
e.printStackTrace(pw);

pw.flush();//立即写入
pw.close();//释放资源


}
}
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值