04 30

Why we write finally block in java

why we write finally block in java

The classical way to program is with try ... catch. When is it appropriate to use try without catch?

In Python the following appears legal and can make sense:

try: #do work finally: #do something unconditional

However, the code didn't catch anything. Similarly one could think in Java it would be as follows:

try { //for example try to get a database connection } finally { //closeConnection(connection) }

It looks good and suddenly I don't have to worry about exception types, etc. If this is good practice, when is it good practice? Alternatively, what are the reasons why this is not good practice or not legal? (I didn't compile the source. I'm asking about it as it could be a syntax error for Java. I checked that the Python surely compiles.)

A related problem I've run into is this: I continue writing the function/method, at the end of which it must return something. However, it may be in a place which should not be reached and must be a return point. So, even if I handle the exceptions above, I'm still returning NULL or an empty string at some point in the code which should not be reached, often the end of the method/function. I've always managed to restructure the code so that it doesn't have to return NULL, since that absolutely appears to look like less than good practice.

Basic try-catch-finally Exception Handling in Java


  1. Zebequr

    ProTip: Try to read a book this summer and keep your mind sharp so that you can head into Fall 2017 with momentum

  2. Hefefosohufek

    JAMB Candidates Block Kaduna Office In Protest Of Inability To Write Exam -

  3. Fexuwowujequke

    In Ukraine many sites block, I was afraid that I will not write, Until it turned out

  4. Goxunayebuzexi

    Do people hit 45 and think, yes, I can block the aisles at grocery stores and farmers markets by having loud conversations?

  5. Xexazowokepe

    Exactly. Some of your writers block people who don"t agree or want to have a open discussion about what they write.

  6. Jomovunulewepo

    Maybe write a cross-browser plugin to override the most common block feature ?

  7. Laqujejoqena

    If he stays you can write next season off, on the pitch and off it because no one is simply going to flip flop and accept him.

  8. Hecoyokajuc

    Parallel Port Status Register ( SSR ) so use 250ms per write block.

  9. Civopumir

    I"d give anything to see write about united and write about Arsenal. A lot of people will block you guys that day

  10. Cuvoniro

    Hi, having trouble with captions for blog post images. Block says "write here" but unable to do so? Vignette template

  11. Guwelov

    Here we go with the unwritten rule bs. Write your rules

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>