Skip to main content

Preventing Java from printing the stack trace on certain exceptions



I have a security class that throws an AccessDeniedException on a user not being authorized for a certain operation. I made it even neat and put a msg in there with the info I need.





The problem is that now my logs are full of stack traces for every single time I throw. I don't care to see the stack trace for THIS exception, and the stack traces are bloating my logs.





Is there a way to tell Java not to print the stack trace to the logs if it's this exception?





Thanks!





--





llappall


Comments

  1. The problem is that now my logs are full of stack traces


    This is like saying "my fire alarm keeps going off, how can I make it quieter?". There are only two good courses of action.


    Determine that these are false alarms, and alter your alarm system so it is not so sensitive to events that are not actually dangerous. The throwing of an exception does not indicate a bug, or even anything very remarkable, if it is a checked exception. Even if a checked exception is worth logging, the stacktrace will not be worth logging, because a stacktrace is useful only for debugging.
    Determine that the alarms are genuine, in which case find out why fires keep breaking out and fix whatever is igniting them. That is, getting fixing the bugs that cause exceptions to be thrown. If you have many bugs that flood your log file it does not matter which you fix first, so just fix whichever are easiest to read in the log file

    ReplyDelete
  2. You need to investigate where you are catching (if catching) this exception, and ensure that you don't log the exception, but only the message...

    ReplyDelete
  3. Either you can decide to catch that particular exception and don't do anything about it.
    Either you can set the UncaughtExceptionHandler on the current Thread.

    Thread.currentThread().setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh)


    and in the handle you perform an instanceof on the exception to see if it is that particular exception

    ReplyDelete
  4. I don't know if this will help you with your logging, but if you are creating an Exception purely to improve the logic and readability of your code, and if you never expect to wonder where the heck it came from, then override the fillInStackTrace() method, like so:

    public class AccessDeniedException extends Exception {
    public synchronized Throwable fillInStackTrace() { return this; }
    }


    This will improve your program's performance--dramatically if you throw a lot of them. It won't eliminate logging. The other answers should help with that, but if they don't your log messages will be much reduced.

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

CCNA 3 Final Exam => latest version

1 . Which security protocol or measure would provide the greatest protection for a wireless LAN? WPA2 cloaking SSIDs shared WEP key MAC address filtering   2 . Refer to the exhibit. All trunk links are operational and all VLANs are allowed on all trunk links. An ARP request is sent by computer 5. Which device or devices will receive this message? only computer 4 computer 3 and RTR-A computer 4 and RTR-A computer 1, computer 2, computer 4, and RTR-A computer 1, computer 2, computer 3, computer 4, and RTR-A all of the computers and the router   3 . Refer to the exhibit. Hosts A and B, connected to hub HB1, attempt to transmit a frame at the same time but a collision occurs. Which hosts will receive the collision jamming signal? only hosts A and B only hosts A, B, and C only hosts A, B, C, and D only hosts A, B, C, and E   4 . Refer to the exhibit. Router RA receives a packet with a source address of 192.168.1.65 and a destination address of 192.168.1.161...