• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

unreachable statement again

 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Here 14 is not reached. Still it does not give an unreachable statement error. Why?
Thanks in advance.
[ December 26, 2002: Message edited by: Valentin Crettaz ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do not take me too seriously here, but maybe the compiler tries to stay one step in front of us by assuming you may someday insert some statements that could throw an exception in your try block.
This particular behavour of the compiler may also be dependent on the implementation of the compiler.
Expert advice, anyone?
-Barry
 
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sun par:


What do you thik about this code, line 6 won't be reached, since 1==1.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Code like if (false) statement; and if (true) statement; are handled specially by the compiler to enable a "conditional compilation" idiom. Provided the statements compile no unreachable statement error will be given by the compiler. Provided 1 != 1 is evaluated to false at compile time, the "conditional-compilation" idiom applies.
-Barry
[ December 26, 2002: Message edited by: Barry Gaunt ]
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JLS 14.20 Unreachable Statements
The following code won't compile. The only construct that accepts false (or some expression that evaluates to false) at compile time is the if construct to enable conditional compilation. All other constructs won't accept false and the compilation will fail if such a case arises.

[ December 26, 2002: Message edited by: Valentin Crettaz ]
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sun,
in the example you give, the code doesn't even compile if you put a checked exception in the catch block. For instance, the following code would not compile since ClassNotFoundException is never thrown within the body of the try block. This is another issue but I just thought it was worth mentioning it.

The reason why Exception is not fully considered as a checked exception has been the source of many debates.
If you put a second return statement in the catch block (between lines 12 and 13), the code won't compile and will give you the following error:

[ December 26, 2002: Message edited by: Valentin Crettaz ]
 
sun par
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Barry, Don, Valentin
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Valentin Crettaz:
The reason why Exception is not fully considered as a checked exception has been the source of many debates.


Valentin
Let me see if I understand this:
If a method explicitly throws an Exception (the exact class), then the Exception must be caught or thrown by the caller, because Exception is a checked exception.
However, any method may throw a RuntimeException without declaring it in its throws clause since RuntimeException it is an unchecked exception. But RuntimeException is also a sub-class of Exception, so a try { methodThrowingRuntimeException(); } catch(Exception e){} block would catch the RuntimeException.
Is that why Java allows us to catch an Exception even when the code in the try block doesn't throw any checked exceptions, including the special case of an empty try block?
Are there any web sites where I can read more about the debates regarding exception handling in Java?
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is that why Java allows us to catch an Exception even when the code in the try block doesn't throw any checked exceptions, including the special case of an empty try block?
You can catch any kind of exceptions (un/checked) and errors in Java. You are just
- required to catch checked exceptions,
- allowed to catch unchecked exceptions and
- discouraged to catch errors.
Are there any web sites where I can read more about the debates regarding exception handling in Java?
I had once mayn links to discussions about this topic but I can only retrieve one of them:
what is the difference between checked and unchecked exceptions
Otherwise I have some useful links:
Java-Help.com: Lesson 47 - Exception handling with try, catch, and finally blocks
JLS 11 Exceptions
 
reply
    Bookmark Topic Watch Topic
  • New Topic