Discussions

General J2EE: try without catch but with finally

  1. try without catch but with finally (2 messages)

    why is that a try block without catch is aceepted if we have a finally block.what will happen if an exception occurs if we dont have have a catch block.why is that allowed(for what purpose)a try without catch but with finally.
  2. try without catch but with finally[ Go to top ]

    Suppose you have a method that opens a file, but youve designed it to throw any IOExceptions it might encounter. You still want to have the finally block there so you can close the file when your finished reading it. This way, if the exception is encountered after the file has been opened, the finally block will still be reached and the file will be closed...then the exception will be thrown.

    Does this help?
  3. try without catch but with finally[ Go to top ]

    Well I see two situations where such a case is required ::

    1. Ur logic doesn't want any thing to happen or be processed after an exception occurs, but then u will like some of the critical steps to be executed ( like releasing resources) irrespective of whatever may happen ...

    2. When u can't figure out the possibility of any exception in ur code (Hence u don't know which exception to catch and how to handle), but the some later steps are very critical and must be excecuted. Hence to play it safe, u use try without catch but with finally.

    There can be many other cases... But I guess above cases are enough for u to be convinced about the idea.

    Cheers,
    Vinayak