Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Using exceptions for early exit

Reply
Thread Tools

Using exceptions for early exit

 
 
Jonathan Bartlett
Guest
Posts: n/a
 
      12-12-2005
I ran across the following article on Java exceptions and noticed this
little tidbit:

"Sometimes developers decide to use exception for flow-control (i.e. to
throw an exception once a certain condition is met). The generation of
stack-traces is expensive and developers should realise that an
exception should only be used in exceptional cases."

(From http://javafoundry.ibscon.com/content/view/18/ )

A few questions:

(1) is this still true? Is a stack trace still expensive?
(2) do all exceptions do this? For example, if I just inherit from
"Throwable" rather than "Exception" is a stack trace still generated?
(3) is there any way around this?

The reason I'm asking is that exceptions would seem to be a very good
way to implement early-exit from certain deeply-nested computations.

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      12-12-2005
Jonathan Bartlett <(E-Mail Removed)> wrote:
> "Sometimes developers decide to use exception for flow-control (i.e. to
> throw an exception once a certain condition is met). The generation of
> stack-traces is expensive and developers should realise that an
> exception should only be used in exceptional cases."
>
> (From http://javafoundry.ibscon.com/content/view/18/ )
>
> A few questions:
>
> (1) is this still true? Is a stack trace still expensive?


Yes, it's still true that generating a stack trace is a relatively
expensive operation, compared to other methods of accomplishing this.
Whether that justifies the generalization above is a different matter.
The number of situations in which small performance costs matters enough
to justify making your life harder is actually rather small.

On the other hand, just from a readability standpoint, use of exceptions
for minor flow control purposes is a poor idea. The ultimate test is
whether there is a substantial body of code in which the exception
actually makes sense and can be given a simple descriptive name.
Generally, when exceptions do get used, there is some context in which
is really IS an exceptional case. Exceptional, in the context of Java,
doesn't mean that it almost never happens, but rather than the code to
handle it is not relevant to the purpose of the method you're writing.

> (2) do all exceptions do this? For example, if I just inherit from
> "Throwable" rather than "Exception" is a stack trace still generated?
> (3) is there any way around this?


Yes, and no.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      12-12-2005


Jonathan Bartlett wrote On 12/12/05 09:57,:
> I ran across the following article on Java exceptions and noticed this
> little tidbit:
>
> "Sometimes developers decide to use exception for flow-control (i.e. to
> throw an exception once a certain condition is met). The generation of
> stack-traces is expensive and developers should realise that an
> exception should only be used in exceptional cases."
>
> (From http://javafoundry.ibscon.com/content/view/18/ )
>
> A few questions:
>
> (1) is this still true? Is a stack trace still expensive?
> (2) do all exceptions do this? For example, if I just inherit from
> "Throwable" rather than "Exception" is a stack trace still generated?
> (3) is there any way around this?
>
> The reason I'm asking is that exceptions would seem to be a very good
> way to implement early-exit from certain deeply-nested computations.


The first two seem the sort of questions you could
answer for yourself with a little experimentation. You'd
get not only answers, but quantitative answers.

The third is unclear to me. What is the "this" you're
worried about, and what would it mean to get "around" it?
That is, what is your problem and what are your criteria
for a successful solution?

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
Andrea Desole
Guest
Posts: n/a
 
      12-12-2005
Jonathan Bartlett wrote:

> The reason I'm asking is that exceptions would seem to be a very good
> way to implement early-exit from certain deeply-nested computations.


If this is your need that probably means that you have to change your
code (for example, you can make methods out of your computations).
If you don't, have a look at break and continue. I personally never use
them (I consider them mostly an extreme solution), but that's what they
are for
 
Reply With Quote
 
ricky.clarkson@gmail.com
Guest
Posts: n/a
 
      12-12-2005
A particular example in the API of using exceptions for flow control is
Integer.parseInt. There's no way of asking whether the int is
parseable,so you have to detect it via an exception.

I think it would be better like this:

IntegerParseResult result=Integer.parseInt("5556");

if (result.hasValue())
doStuff(result.getValue());
else
handleTheBadStuff();

getValue() would throw an IllegalStateException ifcalled when
!result.hasValue().

 
Reply With Quote
 
Jonathan Bartlett
Guest
Posts: n/a
 
      12-12-2005

> The third is unclear to me. What is the "this" you're
> worried about, and what would it mean to get "around" it?


Producing stack traces.

> That is, what is your problem and what are your criteria
> for a successful solution?


Throw something that didn't generate a stack trace.

Jon
 
Reply With Quote
 
Jonathan Bartlett
Guest
Posts: n/a
 
      12-12-2005
> If this is your need that probably means that you have to change your
> code (for example, you can make methods out of your computations).
> If you don't, have a look at break and continue. I personally never use
> them (I consider them mostly an extreme solution), but that's what they
> are for


I was actually looking for a means of a quick escape from
deeply-recursive functions. Just throw the exception to exit the recursion.

Jon
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      12-12-2005


Jonathan Bartlett wrote On 12/12/05 14:02,:
>> The third is unclear to me. What is the "this" you're
>>worried about, and what would it mean to get "around" it?

>
>
> Producing stack traces.
>
>
>>That is, what is your problem and what are your criteria
>>for a successful solution?

>
>
> Throw something that didn't generate a stack trace.


As far as I know, every Throwable contains a stack
trace. You don't need to display it, but it'll be there
no matter what you do.

However, the stack trace is built when the Throwable
is constructed, not by the "throw" action. You could
therefore construct your Exception once at class-loading
time, and then throw the same Exception object as often
as you like:

class Perverse {

private static Exception perverseException
= new Exception();

public void somethingStupid(int n) throws Exception {
if (n <= 0)
throw perverseException;
else
somethingStupid(n-1);
}

public void moreStupidity(double p) throws Exception {
if (Math.random() < p)
throw perverseException;
}
}

Thus, you'd only incur the cost of creating and initializing
the Exception once, instead of at every throw.

I won't reveal what I think about the advisability of the
technique, but the names in the illustration above may provide
a tiny clue ... Have I just given a loaded pistol to a child?

--
(E-Mail Removed)

 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      12-12-2005

"Jonathan Bartlett" <(E-Mail Removed)> wrote in message
news:439dc9c4$(E-Mail Removed)...
>
> I was actually looking for a means of a quick escape from deeply-recursive
> functions. Just throw the exception to exit the recursion.


This sounds like a "Bad Idea".

Does your recursive function return a value? Does it modify some other
data structure? You might be able to "get out of the recursion" more cleanly
by using a "return" statement.

- Oliver


 
Reply With Quote
 
ricky.clarkson@gmail.com
Guest
Posts: n/a
 
      12-12-2005
All recursive operations can be written as iterative. If stack
unwinding irritates you, feel free to rewrite this algorithm to use
iteration.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Any exit status without explicitely using return /exit Keith Thompson C Programming 10 03-03-2010 12:41 AM
Re: Any exit status without explicitely using return /exit jacob navia C Programming 3 02-24-2010 02:39 PM
Exit code of a batch (using exit /B) Joe Smith Java 4 11-08-2006 12:25 PM
Exit Custom Server Control Early Marc Miles ASP .Net 2 08-17-2005 05:20 PM
jython: how do you exit an applet early? gcash Python 0 02-20-2004 03:17 AM



Advertisments