> Also, you should not catch Exception, catch the specific type of
> exception you really want. But I assume this is just an example. I'm
> going to change the type of Exception anyway, it really bugs me.
Yes, I know that catching a generic exception is considered bad form,
and yes, I just simplified things for the example.
I guess my question is, and granted I haven't tried it with the 'throw
e' - but where does the return statement go? At the very bottom
(outer most scope) of the method? I originally had it in the try{}
block, but the compiler complained.
thanks!
Mark Space - 17 Apr 2008 17:49 GMT
> I guess my question is, and granted I haven't tried it with the 'throw
> e' - but where does the return statement go? At the very bottom
> (outer most scope) of the method?
I prefer at the very end. Only if the logic of the method is very
simple is it ok to embed a return in the body of a method. Or if
embedding the return simplifies the logic.
The reason here is just to make it simple to read. The compiler is
allowed to rearrange things to make them more efficient. Trust it to do
its job.
> I originally had it in the try{}
> block, but the compiler complained.
>
> thanks!
Move it back to the try{} block. Add the "throw e;" that I had in to
the catch{} block. See if that helps.
Arved Sandstrom - 17 Apr 2008 18:25 GMT
>> Also, you should not catch Exception, catch the specific type of
>> exception you really want. But I assume this is just an example. I'm
[quoted text clipped - 9 lines]
>
> thanks!
The compiler complained because there must be a reachable return. If you
stick your return in the try block and there's an exception beforehand you
won't have a return. There doesn't need to be a return statement in the
outermost scope, no, not if all the possible execution paths have a return
in them. Try an if-else and put returns in there, and not in the outermost
scope of the method - it will compile.
With try blocks, bear in mind that a return in a try won't, not if you have
a finally clause also. And don't use return in a finally clause...although
you can. Mind you, you'll get warned about the latter.
You can have (legally) a return in the try block, and the compiler won't
complain, not if there is no code prior that will throw an exception.
Having said all that, what's best practise, exceptions or not? Many people
will say that multiple return statements in a method are bad, others strive
for a single return statement (which under normal circumstances but not
always) would be in outermost method scope but occasionally use multiple
returns, and others sprinkle returns everywhere. I myself would sort of go
with Bruce Eckel here:
http://onthethought.blogspot.com/2004/12/multiple-return-statements.html
AHS
Brian Munroe - 21 Apr 2008 16:35 GMT
On Apr 17, 10:25 am, "Arved Sandstrom" <asandst...@accesswave.ca>
wrote:
> Having said all that, what's best practise, exceptions or not? Many people
> will say that multiple return statements in a method are bad, others strive
> for a single return statement (which under normal circumstances but not
> always) would be in outermost method scope but occasionally use multiple
> returns, and others sprinkle returns everywhere. I myself would sort of go
> with Bruce Eckel here:http://onthethought.blogspot.com/2004/12/multiple-return-statements.html
Thank you for a very concise answer regarding the compiler errors. If
I would have thought about it more, I could have figured out what it
was saying, but thanks for affirming what I was already thinking.
Also, I've read the Bruce Eckel blog post, I think that makes sense
and I think I'll adopt it.