Re: can't throw
On 12.09.2012 21:52, Lew wrote:
bob smith wrote:
Robert Klemme wrote:
Why are you subclassing Thread in the first place?
There are two ways to create a new thread of execution. One is to declare a class to be a subclass of
I find the wording of this answer unfortunate: you create a thread of
execution by invoking Thread.start(). Bob, what you probably meant was
there are two ways to create functionality executed in a separate
thread. But then Lew's comment applies:
There are far more than two ways.
Thread. This subclass should override the run method of class Thread. An instance of the subclass
can then be allocated and started. For example, a thread that computes primes larger than a stated
value could be written as follows:
You explained what you're doing, but you didn't answer Robert's question of why.
Exactly. Again: why are you doing this, Bob?
Why are you subclassing Thread in the first place?
Most people's answers in this thread revolve around the fact how a
method without declared checked exceptions can be made to throw
exceptions anyway. But: there is no point in throwing from Thread.run()
or Runnable.run() other than catastrophic failures (all Errors such as
OOM, all RuntimeExceptions which are really programmer mistakes) because
you cannot customize handling of exceptions thrown from the run()
method: this method is only invoked from class Thread and probably
others in the Java standard library itself. Instead, you should be
handling exceptions in run() and terminate the thread gracefully (i.e.
by returning from run()).
It's abnormal and you aren't doing anything that obviously calls for this idiom.
"Because I can!" only answers a certain question asked of a male dog.
:-)
Kind regards
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/