Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

return in catch block?

 
Jon Dornback
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i've recently seen several code examples that have a return statement within a catch block. am i correct in assuming that that is the return statement for the method the code is in? or does that return something from within the catch block?
corollary: can break and continue statements be used within try/catch blocks? or only for loop structures?
thanks
Jon
 
Max Habibi
town drunk
( and author)
Sheriff
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jon,
Yes, you return from a catch block. You can also return from a finally block.
No, you can't break out of a catch the way you can out of a loop. However, you _can_ throw an exception from a catch block.
HTH,
M, author
The Sun Certified Java Developer Exam with J2SE 1.4
 
Peter Phung
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The value you return doesn't need to be generated from within the method itself. The value can be declared globally and then returned from the method.

i hope this helps.
[ July 24, 2002: Message edited by: Peter Phung ]
 
Daniela Ch
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it bad style is we return from inside a for loop?
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's VERY bad style to return in a try/catch block -- what happens in this code (from JavaOne 2002 TS-2500 Progamming Puzzlers)??

So... what's the result? plop it in a file and run it.
[ December 19, 2002: Message edited by: Jessica Sant ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jessica Sant:
It's VERY bad style to return in a try/catch block

I don't think this is true generally - for example, I think the following would be perfectly ok:
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Ilja. There are those who insist that it's bad form to have a return anywhere but the end of a method, or to use continue or break anywhere but in a switch. However in reality there are sometimes perfectly valid reasons to break these rules. Just beware that you are doing something which is at least unusual, and so it may be harder to understand for unexperienced programmers. I strongly recommend keeping each method very short (calling other methods as necessary) so that it's easy for people to see how it works. This, IMO, is far more useful than blindly obeying rules like "don't return inside a try statement".
Having said that, I will add that it's almost always a bad idea to return from inside a finally clause. That's because the return causes the JVM to forget what it was doing before it executed finally - if you were supposed to be catching an exception, that exception is now lost with no trace. Which is very, very bad. It's possible someone can think of an example where a return inside a finally is acceptable or even useful - but I doubt it.
[ December 20, 2002: Message edited by: Jim Yingst ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, regarding the original post:
can break and continue statements be used within try/catch blocks? or only for loop structures?
Yes, it's possible to have break or continue inside a try/catch - but only if it is also inside a loop of some sort, which is either inside the try or catch, or which wraps around the try/catch. E.g.

The break is associated with the loop, not the try/catch, but it's still inside a try/catch...
[ December 20, 2002: Message edited by: Jim Yingst ]
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Jessica. It's possible and people do it, but personally I hate the idea, I think it's dangerous, and it isn't that much of an effort to code around needing it.
To extend the code sample she gave, returning from a catch block doesn't necessarily cause problems, it more to do with behaviour when a finally block is there too. If you have a catch block with a return, you may not be able to safely add a finally block without rewriting the code. If the catch doesn't return, there is less chance you'll get in trouble.
If you return and re-throw exceptions in catch blocks and return from finally blocks, you can create code where it is difficult to know what is going on.
Dave
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Daniela Ch:
Is it bad style is we return from inside a for loop?

Returning to the original hijack and continuing from my previous post:
It is a good idea to only have a single return statement in a method that requires one. It (generally) makes the method easier to maintain.
The 'generally' refers to the counter-argument where a method that is going to fail should fail quickly.

So Dave says: you can return in catch blocks and in inside 'for' loops, but if you can avoid it, don't.
Dave
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

easier to maintain than

???
If so, why? To me, the latter seems to be much simpler...

you can return in catch blocks and in inside 'for' loops, but if you can avoid it, don't.

This seems to be a rather strange advice, as it would be *always* possible to avoid it, wouldn't it?
[ December 21, 2002: Message edited by: Ilja Preuss ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, heck... for this particular case:

Of course, there are those who find ternary operators upsetting and confusing as well. But at some point I think we do other programmers a disservice by coddling them too much.
I often find the "returnValue" pattern annoying anyway. It's one thing if you give the variable some other name that actually tells people what its significance is, like isValidData or hasDuplicates. That serves a useful purpose. But a name that says "this is the value I'm going to return later at the very end of the method because I'm not supposed to put a return statement here"? :roll: Just return the value and move on.
On the subject of returning withing try/catch: note that in the link Jessica gave, Joshua Bloch gives an example of returning from inside a finally, and shows why it's bad. At the end of that section, under the "Moral" section, he says: avoid abrupt completion of finally blocks. Doesn't say anything about not returning from inside a try or catch though. And in fact you can find several examples of returns from inside try or catch, in Josh's book, Effective Java. (As well as numerous examples of multiple returns.) Not that everyone has to blindly follow Josh's example of course - but since Jessica brought him up...
[ December 22, 2002: Message edited by: Jim Yingst ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, the example certainly was simplified to a degree that it wasn't very usefull. How about the following?

vs.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tend to find it a bit of a catch-22 though. If a method is large and has several exits points, it is difficult to trace the flow if several other methods act the same.
The other side is that if a large method decides on the return value early on, it needs to protect that value from alteration later on
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what you mean by the second point. But for the first, I agree. I think the difference is that I think all long methods are evil (well, most of them, especially if they have complicated nesting or internal returns) and you think the internal returns are evil (especially if the method is long). Not too far apart, I think.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David O'Meara:
If a method is large and has several exits points, it is difficult to trace the flow if several other methods act the same.

Interestingly, refactoring such a method so that it only has one exit point typically only makes it even larger...
So it is my experience that it isn't multiple exit points you should avoid, but large methods!
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In case anybody else had been looking for it:
http://www.c2.com/cgi/wiki?SingleFunctionExitPoint
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic