This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with Try-Catch-Finally statement

 
Raistlin Zischer
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, here's want I am needing help on. I am doing a try-catch-finally statement (obviously). I am running into the problem where the finally statement is being ran, even though the catch statement is ran and I have put a return statement in the statement. I know that the finally statement won't run if there is a System.exit() is in the catch statement before the finally statement. Does return not cause a System.exit() in the catch statement, or do I need to actually have it say System.exit() after the return?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The whole point of a "finally" block is that it runs after the try or catch completes (or if they all abort). If you don't want the "finally" code to execute if the catch block runs, then don't put the code in "finally" -- put it into the "try". Or if you want the "finally" code to run before the catch block, then use a separate try/finally nested inside your try/catch.

System.exit halts the JVM altogether. It's unrelated to "return". If the JVM halts, obviously no more code is executed.

Finally, this is the "IDEs" forum, for talking about tools like Eclipse and WSAD. It's not for talking about Java code issues. I'm going to move this to Java in General (Intermediate), where we can continue this discussion if you'd like.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Ernest said. I am not sure why you would put code in a finally if you don't want to execute it when there is an exception. Either put it in the try or move it outside of the whole try..catch statement altogether.
 
Raistlin Zischer
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I thought the Finally statement would only run if the none of the catches were ran. What I am doing is running several ojb Criterias and sending them a certain String that the user has entered. The string can be anything, but the columns the criteria are going to be checking are of different types....varchar, int, and bigInt. I need to see if I can cast the string as one of the corresponsing java types (Try statement), if not, then just ran the criteria that can take the casts that worked (Catch), but if I can then send the criteria the proper type and run the searches(Finally). What would be the best way to go about this then if I am doing it worng with the try catch finally block.
And sorry about the entry into the wrong forum. I put it there because I am using Eclipse as the IDE tool and wasn't sure if the try block would be different for other IDE's.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best way to do this would be not to use exceptions at all. Exception handling should be used to catch problems, never as part of normal control flow. Use the "instanceof" operator, which returns true if a cast would succeed; i.e.,

Object something = someWayOfGettingAnUnknownObject();

This is not only cleaner and easier to understand, it's a zillion times more efficient. Creating and throwing an exception object is quite an expensive operation.
 
Raistlin Zischer
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I didn't even think to use the instanceOf, and yeah, the Exception object is indeed costly to use. I'll try the instanceOf and let you know how it turns out. Thanks again for the help.
 
Raistlin Zischer
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, let me see if I have this right.


Correct me if I'm wrong.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic