aspose file tools*
The moose likes Java in General and the fly likes Help with Try-Catch-Finally statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Help with Try-Catch-Finally statement" Watch "Help with Try-Catch-Finally statement" New topic
Author

Help with Try-Catch-Finally statement

Raistlin Zischer
Greenhorn

Joined: Jul 05, 2005
Posts: 25
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Raistlin Zischer
Greenhorn

Joined: Jul 05, 2005
Posts: 25
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Jul 05, 2005
Posts: 25
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

Joined: Jul 05, 2005
Posts: 25
Ok, let me see if I have this right.


Correct me if I'm wrong.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with Try-Catch-Finally statement