0

我正在使用这段代码:

Logger.getLogger( SampleAction.class.getName() ).log( Level.SEVERE, null, ex );

用于在 Logger 中记录异常。

除了记录异常,它还在 UI 中显示异常。

如何避免显示此异常?

4

2 回答 2

0

如何创建另一个这样的类。

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());
}

你可以修改它,如果你为每个日志写日期和时间会更好。

于 2012-12-20T06:17:12.183 回答
0

有两件事发生:

  1. 记录器正在记录异常
  2. 在另一段代码将其显示在对话框中之前,不会捕获或传播异常。如果您不希望显示对话框,则需要在异常显示之前对其进行处理。

请粘贴包含日志记录的整个方法。

于 2012-12-21T23:43:13.690 回答