0
点赞
收藏
分享

微信扫一扫

JAVA中抓异常的办法

有点d伤 2022-01-28 阅读 63


  前几天有人说软件出错了,又不知道哪里出错。然后吾就想自己抓异常。

  昨天终于闲了下来,于是就重操旧业。

public class UnExceptionHandler implements Thread.UncaughtExceptionHandler
{

private static UnExceptionHandler exceptionHandler;
private static long lastTime;
private static long File logFile;

public static UnCatchHandler getInstance()
{
if(mUnCatchHandler == null)
{
synchronized (UnCatchHandler.class)
{
exceptionHandler = new UnExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(exceptionHandler);
lastTime = System.current();
logFile = new File();
}
}
return exceptionHandler;
}

private UnCatchHandler(Context context)
{
//
}


/**
* 保存异常
* @param t
* @param e
*/
@Override
public void uncaughtException(Thread t, Throwable e)
{
//时间控制,文件大小控制
try
{
BufferedWriter writer = new BufferedWriter(new FileWriter(logFile));
witer.write(time);
for (ele : e.get())
{
writer.write();
}
writer.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}



举报

相关推荐

0 条评论