我正在使用这段代码:
Logger.getLogger( SampleAction.class.getName() ).log( Level.SEVERE, null, ex );
用于在 Logger 中记录异常。
除了记录异常,它还在 UI 中显示异常。
如何避免显示此异常?
如何创建另一个这样的类。
public class Log{
FileConnection fc;
OutputStream outStream;
InputStream inStream;
Log(String exceptionClass, String errorMessage){
try{
fc = (FileConnection)Connector.open("[path]");
if(!fc.exists()) fc.create();
inStream = fc.openInputStream();
outStream = fc.openOutputStream();
if(inStream!=null)
outStream.write(IOUtilities.streamToBytes(instream)); //use this so overwriting is enabled
outStream.write((exceptionClass+"\n").getBytes());
outStream.write((errorMessage+"\n").getBytes());
}
catch(IOException e){}
finally{
try{
inStream.close();
outStream.close();
fc.close();
}catch(Exception e){}
}
}
}
然后只需在 try 块的每个捕获处调用您的 Log 类:
例如
try{
//your process
}catch(Exception e){
//call log
new Log(e.getClassName(), e.getMessage());
}
你可以修改它,如果你为每个日志写日期和时间会更好。
有两件事发生:
请粘贴包含日志记录的整个方法。