0

I am using this code:

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

for logging the exceptions in the Logger.

Along with logging the exception, it is displaying the Exception in the UI.

How to avoid showing this exception?

StanislavL
  • 56,971
  • 9
  • 68
  • 98
Nigel Thomas
  • 1,881
  • 8
  • 30
  • 50
  • When you say UI, do you mean the console? – Romski Dec 20 '12 at 06:02
  • Romski, It is a swing based application, so it is showing exception in the UI in a dialog box – Nigel Thomas Dec 20 '12 at 06:05
  • can you put some more input.. – Sumit Singh Dec 20 '12 at 06:06
  • 1
    That sounds more like either a feature of your system/application or `Logger` – MadProgrammer Dec 20 '12 at 06:11
  • Is that exception something that cannot be caught and handled ? – prajeesh kumar Dec 20 '12 at 06:11
  • http://stackoverflow.com/questions/75218/how-can-i-detect-when-an-exceptions-been-thrown-globally-in-java – Jayan Dec 20 '12 at 06:22
  • Tq all for the replies.. – Nigel Thomas Dec 20 '12 at 06:23
  • Sumit: I need to log the exception in the log file. When the exception happens, right now, it is logging into the logger plus it is showing in the UI in the form of a dialog box – Nigel Thomas Dec 20 '12 at 06:31
  • MadProgrammer: Logger will log the exception in the log in the log file the whole stacktrace if the ex is passed into the log() method, If ex.getMessage() is passed it wont shoe the exception dialog box, but will log only the first line in the log file. – Nigel Thomas Dec 20 '12 at 06:33
  • If you use `@name` in a comment (instead of just `name`, Stackoverflow will notify the person you're replying to that you've said something to them. The person who wrote the answer or question you're commenting on always gets notified, so I don't need to say `@nigelthomas` for you to be notified, though. – simont Dec 22 '12 at 00:51

2 Answers2

0

How about create another class like this.

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){}
            }
      }

}

Then just call your Log class at every catch of your try block:

e.g.

try{
      //your process
}catch(Exception e){
       //call log
       new Log(e.getClassName(), e.getMessage());
}

you can modify it, better if you write date and time for each log.

Jj Tuibeo
  • 773
  • 4
  • 18
  • I am trying it... Will let you know soon. – Nigel Thomas Dec 20 '12 at 06:23
  • Jj Tuibeo. I tried your suggestion, but the problem is I need to show the entire Exception like shown in the stack trace. But in this case it shows only the first line of the stack trace. I think this is happening because we are using e.getMessage(). – Nigel Thomas Dec 20 '12 at 06:27
  • i see. getMessage() will not give you the whole stacktrace. i'm not sure but i thinkthere are some methods to convert the stacktrace to string. – Jj Tuibeo Dec 20 '12 at 06:37
  • 1
    http://stackoverflow.com/questions/1149703/stacktrace-to-string-in-java try this if you want to convert the stack trace to string to write it in a separate file. – Jj Tuibeo Dec 20 '12 at 06:39
0

There are two things happening:

  1. The logger is logging an exception
  2. The exception is not being caught, or is being propogated, until another piece of code displays it in a dialog box. If you don't want the dialog box shown you will need to handle the Exception before it gets displayed.

Please paste the whole of the method that contains the logging.

Romski
  • 1,912
  • 1
  • 12
  • 27