Hi all,
Can anyone please confirm if using a static, pre-created Exception
object in a throw statement across multiple threads, thread-safe? I
have a need to use a pre-created Exception to improve the performance.
I use pre-created exception in production mode, where as new Exception
object in debug mode.
Line 14 in the sample test program below highlights this. Is Line 14,
thread-safe?
1 public class StaticPreCreatedExceptionTest implements Runnable
{
2 private static final Exception ex = new Exception("static
pre-created exception");
3
4 private String tid = null;
5
6 public StaticPreCreatedExceptionTest(String tid) {
7 this.tid = tid;
8 }
9
10 public void run() {
11 while ( true ) {
12 try {
13 // is this thread safe?
14 throw ex;
15 }
16 catch (Exception e) {
17 System.out.println(tid + ": caught exception");
18 }
19 }
20 }
21
22 public static void main(String args[]) throws Exception {
23 int MAXTHREADS = 5;
24
25 Thread threads[] = new Thread[MAXTHREADS];
26 for ( int i = 0; i < MAXTHREADS; i++ ) {
27 threads[i] = new Thread(new
StaticPreCreatedExceptionTest("t" + i));
28 threads[i].start();
29 }
30
31 for ( int i = 0; i < MAXTHREADS; i++ ) {
32 // wait until all threads are done
33 threads[i].join();
34 }
35 }
36 }
I thought it was thread-safe, but current thread stack trace of the
Java core dumps in my software always points to the "throw" statement.
So wanted to confirm from someone.
Thanks,
Shailender
Chris Smith - 18 Mar 2005 06:29 GMT
> Can anyone please confirm if using a static, pre-created Exception
> object in a throw statement across multiple threads, thread-safe? I
[quoted text clipped - 4 lines]
> Line 14 in the sample test program below highlights this. Is Line 14,
> thread-safe?
Yes, it should be. The same exception may be thrown by several
different threads at the same time without difficulty. However, if you
modify the state of the exception object, then you may run into
problems.
Incidentally, if creating an Exception object is a performance-critical
operation in your code, you have VERY serious problems that you ought to
resolve immediately. Optimizing the process of throwing the exception
is not to right way to solve those problems. Fixing your abuse of
exceptions would be better.

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Tony Morris - 18 Mar 2005 08:44 GMT
> Hi all,
>
[quoted text clipped - 53 lines]
> Thanks,
> Shailender
Yes.
However, your statement, " I have a need to use a pre-created Exception to
improve the performance." is called an anti-pattern (where you mean to
achieve something, but you actually do the opposite). The antipattern has
been "dubbed" premature optimization.
In any case, object creation is cheap albeit common misconception to the
contrary.

Signature
Tony Morris
http://xdweb.net/~dibblego/
Tor Iver Wilhelmsen - 18 Mar 2005 16:00 GMT
> Can anyone please confirm if using a static, pre-created Exception
> object in a throw statement across multiple threads, thread-safe?
As others have pointed out: Yes, as long as you don't change the state.
However, a word of warning: People often like the stack trace to be
useful in debugging: Your static exception will not be, since it's
unrelated to where it was thrown.
> I have a need to use a pre-created Exception to improve the
> performance.
Exceptions should be relatively rare (hence the name), so a
microsecond here or there is no preformance hit.
Thomas Schodt - 18 Mar 2005 18:10 GMT
> using a static, pre-created Exception
> object in a throw statement
The only remotely valid reason I can think of
would be an OutOfMemoryException
because the VM might not be able to create one if it is OOM,
or if it can the new OOME might be incomplete
(could be missing the stack trace array).
Could work as a quick debug-aid hack. Maybe.
> need to use a pre-created Exception to improve the performance.
Well, at least we got a good laugh out of that one.
Shailender Bathula - 23 Mar 2005 05:34 GMT
Thank you all for your responses. Point against using pre-created
exception is taken.
Shailender Bathula - 23 Mar 2005 06:25 GMT
Thanks for all your responses. Point against using pre-created
exceptions is taken.
> > using a static, pre-created Exception
> > object in a throw statement
[quoted text clipped - 10 lines]
>
> Well, at least we got a good laugh out of that one.
Thomas Schodt - 05 Apr 2005 01:45 GMT
> Can anyone please confirm if using a static, pre-created Exception
> object in a throw statement across multiple threads, thread-safe? I
> have a need to use a pre-created Exception to improve the performance.
> I use pre-created exception in production mode, where as new Exception
> object in debug mode.
If your design is littered with trivial exceptions
you can disable exception stack trace generation
-XX:-StackTraceInThrowable
Not nice. But if you cannot change the design, this certainly beats
having different code for dev & prod.
Thomas Schodt - 12 Apr 2005 10:24 GMT
> If your design is littered with trivial exceptions
> you can disable exception stack trace generation
> -XX:-StackTraceInThrowable
>
> Not nice. But if you cannot change the design, this certainly beats
> having different code for dev & prod.
It appears Tiger (in -server mode) will do something like this for
exceptions that happen repeatedly, controlled by
-XX:+OmitStackTraceInFastThrow
-XX:-OmitStackTraceInFastThrow