Is there any sense in using final keyword in catch block?

Discussion in 'Java' started by Royan, Oct 23, 2008.

  1. Royan

    Royan Guest

    The question is simple, does it make sense to add final keyword like
    in the example below?

    try {
    // code
    } catch (final XxxxException e) {
    // do exception handling
    }

    Whether it makes sense or not, please justify your answer.
    Royan, Oct 23, 2008
    #1
    1. Advertising

  2. Royan wrote:
    > The question is simple, does it make sense to add final keyword like
    > in the example below?
    >
    > try {
    > // code
    > } catch (final XxxxException e) {
    > // do exception handling
    > }
    >
    > Whether it makes sense or not, please justify your answer.


    Homework?

    --
    RGB
    RedGrittyBrick, Oct 23, 2008
    #2
    1. Advertising

  3. Royan wrote:
    > The question is simple, does it make sense to add final keyword like
    > in the example below?
    >
    > try {
    > // code
    > } catch (final XxxxException e) {
    > // do exception handling
    > }
    >
    > Whether it makes sense or not, please justify your answer.


    Does it make sense to add the final keyword to a local variable?

    Do you know what the ramifications of doing so are?

    If you can answer those two questions, you should be able to translate
    the semantics of the final keyword to the example and explain why it
    would or would not make sense.

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
    Joshua Cranmer, Oct 23, 2008
    #3
  4. Royan

    Royan Guest

    On 23 ÏËÔ, 18:44, Joshua Cranmer <> wrote:
    > Royan wrote:
    > > The question is simple, does it make sense to add final keyword like
    > > in the example below?

    >
    > > try {
    > > š // code
    > > } catch (final XxxxException e) {
    > > š // do exception handling
    > > }

    >
    > > Whether it makes sense or not, please justify your answer.

    >
    > Does it make sense to add the final keyword to a local variable?
    >
    > Do you know what the ramifications of doing so are?
    >
    > If you can answer those two questions, you should be able to translate
    > the semantics of the final keyword to the example and explain why it
    > would or would not make sense.
    >
    > --
    > Beware of bugs in the above code; I have only proved it correct, not
    > tried it. -- Donald E. Knuth


    Guys this is a serious question and is actually aimed to reveal any
    JIT optimizations (if any), best practices or whatever.

    I've read a couple of articles regarding the usage of final keyword,
    and to the moment the most interesting point I've read regarding its
    usage in the catch block was about upcoming changes in java. Check out
    this article: http://weblogs.java.net/blog/emcmanus/archive/2008/05/javaone_report.html
    Royan, Oct 23, 2008
    #4
  5. Royan

    Daniel Pitts Guest

    Royan wrote:
    > The question is simple, does it make sense to add final keyword like
    > in the example below?
    >
    > try {
    > // code
    > } catch (final XxxxException e) {
    > // do exception handling
    > }
    >
    > Whether it makes sense or not, please justify your answer.

    It is often a stylistic thing. My personal style is to make fields and
    local variables final, but leave parameters/catch as not final.

    This is completely arbitrary. My ideal would have been if Java had made
    every field/parameter/local final by default, and the programmer could
    specific nonfinal for things they really wanted to change.



    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
    Daniel Pitts, Oct 23, 2008
    #5
  6. Royan wrote:
    > Guys this is a serious question and is actually aimed to reveal any
    > JIT optimizations (if any), best practices or whatever.


    Your original question sounds copied verbatim from some homework
    assignment. As I said earlier, a final in a catch statement means the
    same as any other final local variable.

    > I've read a couple of articles regarding the usage of final keyword,
    > and to the moment the most interesting point I've read regarding its
    > usage in the catch block was about upcoming changes in java. Check out
    > this article: http://weblogs.java.net/blog/emcmanus/archive/2008/05/javaone_report.html


    That's not upcoming changes:
    "They followed with two proposals for exception handling..."

    I doubt that the second proposal (the one that changes the implication
    of final) would be accepted over the first one.

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
    Joshua Cranmer, Oct 23, 2008
    #6
  7. Daniel Pitts wrote:
    > Royan wrote:
    >> The question is simple, does it make sense to add final keyword
    >> like
    >> in the example below?
    >>
    >> try {
    >> // code
    >> } catch (final XxxxException e) {
    >> // do exception handling
    >> }
    >>
    >> Whether it makes sense or not, please justify your answer.

    > It is often a stylistic thing. My personal style is to make fields
    > and local variables final, but leave parameters/catch as not final.
    >
    > This is completely arbitrary. My ideal would have been if Java had
    > made every field/parameter/local final by default, and the
    > programmer
    > could specific nonfinal for things they really wanted to change.


    for (nonfinal int i = 0; i < 10; i++)

    Yup, a *big* improvement.
    Mike Schilling, Oct 23, 2008
    #7
  8. Royan

    Tom Anderson Guest

    On Thu, 23 Oct 2008, Mike Schilling wrote:

    > Daniel Pitts wrote:
    >> Royan wrote:
    >>
    >>> The question is simple, does it make sense to add final keyword like
    >>> in the example below?
    >>>
    >>> try {
    >>> // code
    >>> } catch (final XxxxException e) {
    >>> // do exception handling
    >>> }
    >>>
    >>> Whether it makes sense or not, please justify your answer.

    >> It is often a stylistic thing. My personal style is to make fields
    >> and local variables final, but leave parameters/catch as not final.


    My style is to not use final for local variables (including parameters and
    catch variables) at all. Unless they're being closed over by an anonymous
    class, where it's required. I think the point of a final qualifier is to
    reassure the programmer (reading or writing the code) that it won't change
    unexpectedly - it's an aid to reasoning about the code. Instance variables
    can be read and written from anywhere in the class (or even outside, if
    they're not private), and thus in a class of more than trivial size, it
    can be hard to keep track of whether that's happening, and where.
    Declaring a field final eliminates this difficulty at a stroke. However,
    local variables can only be accessed from inside the method they're local
    to; that's a much smaller body of code that the programmer has to keep in
    mind to know everything about the variable's use. There, i don't think
    there's much uncertainty to be reduced, and the additional keywords just
    clutter up the screen and mean more letters to type.

    Note that for this analysis to hold, your methods have to be short: small
    enough that you can hold the whole thing in your mind at once. However, i
    would say that this is exactly how methods should be. If your method is
    big enough that making its locals final is helping you, then you need to
    split it up into multiple smaller methods. Essentially, final locals are a
    code smell.

    >> This is completely arbitrary. My ideal would have been if Java had
    >> made every field/parameter/local final by default, and the programmer
    >> could specific nonfinal for things they really wanted to change.


    I quite like this idea too. I'd want to see how it felt in practice before
    i fully supported it, though.

    > for (nonfinal int i = 0; i < 10; i++)
    >
    > Yup, a *big* improvement.


    Even better, IMHO:

    for (variable int i = 0; i < 10; i++)

    !

    tom

    --
    Virtually everything you touch has been mined. -- Prof Keith Atkinson
    Tom Anderson, Oct 23, 2008
    #8
  9. Royan

    Lew Guest

    Tom Anderson wrote:
    > My style is to not use final for local variables (including parameters and
    > catch variables) at all. Unless they're being closed over by an anonymous
    > class, where it's required. I think the point of a final qualifier is to
    > reassure the programmer (reading or writing the code) that it won't change
    > unexpectedly - it's an aid to reasoning about the code. Instance variables
    > can be read and written from anywhere in the class (or even outside, if
    > they're not private), and thus in a class of more than trivial size, it
    > can be hard to keep track of whether that's happening, and where.
    > Declaring a field final eliminates this difficulty at a stroke. However,
    > local variables can only be accessed from inside the method they're local
    > to; that's a much smaller body of code that the programmer has to keep in
    > mind to know everything about the variable's use. There, i don't think
    > there's much uncertainty to be reduced, and the additional keywords just
    > clutter up the screen and mean more letters to type.


    There are also implications of 'final' for thread safety.

    --
    Lew
    Lew, Oct 23, 2008
    #9
  10. Royan

    Daniel Pitts Guest

    Lew wrote:
    > Tom Anderson wrote:
    >> My style is to not use final for local variables (including parameters and
    >> catch variables) at all. Unless they're being closed over by an anonymous
    >> class, where it's required. I think the point of a final qualifier is to
    >> reassure the programmer (reading or writing the code) that it won't change
    >> unexpectedly - it's an aid to reasoning about the code. Instance variables
    >> can be read and written from anywhere in the class (or even outside, if
    >> they're not private), and thus in a class of more than trivial size, it
    >> can be hard to keep track of whether that's happening, and where.
    >> Declaring a field final eliminates this difficulty at a stroke. However,
    >> local variables can only be accessed from inside the method they're local
    >> to; that's a much smaller body of code that the programmer has to keep in
    >> mind to know everything about the variable's use. There, i don't think
    >> there's much uncertainty to be reduced, and the additional keywords just
    >> clutter up the screen and mean more letters to type.

    >
    > There are also implications of 'final' for thread safety.


    It is truly unfortunate that Java hasn't the same concept of "const
    reference" that C++ does. It would make Java code so much easier to
    verify as thread-safe and otherwise bug-free.

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
    Daniel Pitts, Oct 23, 2008
    #10
  11. Royan

    Arne Vajhøj Guest

    Royan wrote:
    > The question is simple, does it make sense to add final keyword like
    > in the example below?
    >
    > try {
    > // code
    > } catch (final XxxxException e) {
    > // do exception handling
    > }
    >
    > Whether it makes sense or not, please justify your answer.


    I consider it unnecessary cluttering of the code.

    Arne
    Arne Vajhøj, Oct 25, 2008
    #11
  12. Royan

    Arne Vajhøj Guest

    Daniel Pitts wrote:
    > It is truly unfortunate that Java hasn't the same concept of "const
    > reference" that C++ does. It would make Java code so much easier to
    > verify as thread-safe and otherwise bug-free.


    I also miss that a lot.

    But I am not sure how easy it would be to implement with all
    those libraries that use reflection.

    Arne
    Arne Vajhøj, Oct 25, 2008
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Timo Nentwig
    Replies:
    31
    Views:
    1,202
    Chris Smith
    May 13, 2004
  2. JFCM
    Replies:
    4
    Views:
    5,732
  3. Exits Funnel
    Replies:
    1
    Views:
    317
    Sharad Kala
    Jan 8, 2005
  4. morrell
    Replies:
    1
    Views:
    946
    roy axenov
    Oct 10, 2006
  5. Ricardo Palomares Martinez
    Replies:
    2
    Views:
    375
    david.karr
    Apr 28, 2007
Loading...

Share This Page