This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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?
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.
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.
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.
author and iconoclast
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.,