• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception error

 
M Rama
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I do the following:



results in an error "unreachable block" at the catch statement. However:




doesnot cause error. Why?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's an interesting question.

Normally, the compiler guards against catch blocks that can never be entered. But there (apparently) comes a point where that's just not practical.

It's virtually impossible for the compiler to predict what type of code might throw any type of Exception (including RuntimeException). So basically, the compiler will "look the other way" and let a catch(Exception) block pass, even if the associated try block is empty.

On the other hand, your own class, MyException, is much more specific, so it's much more reasonable for the compiler to check for code that might throw an instance of MyException.

See this thread:
http://www.coderanch.com/t/375599/java/java/Exception-class-special
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, have a look at Bug ID: 4046575 .
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joyce Lee:
Also, have a look at Bug ID: 4046575.

I'm glad to know it's officially a bug.

But after 8 years of not getting fixed, maybe they ought to consider rewriting the Language Specs to accept this as a "feature."

:roll:
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi lee

can i say in the following program the line (1) unreachable statement.

is that mandatory that there should be some statement,expression or something should placed in line (1) to get the unreachable statement
[ April 07, 2005: Message edited by: Parameswaran Thangavel ]
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
any one please
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anyone please
 
Rahul Bhosale
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i dont think the code will even compile. but if you catch MyException before Exception it compiles and prints catch 2 End

following is the updated code

but if you catch Exception before MyException results in a compilation error saying exception block Unreachable since the exception is already caught by a super class.
[ April 08, 2005: Message edited by: Rahul Bhosale ]
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Parameswaran,

Assuming that the line "throws new MyException" is corrected, line (1) can be considered an unreachable statement.

From JLS 14.20:

A catch block C is reachable iff both of the following are true:
  • Some expression or throw statement in the try block is reachable and can throw an exception whose type is assignable to the parameter of the catch clause C. (An expression is considered reachable iff the innermost statement containing it is reachable.)
  • There is no earlier catch block A in the try statement such that the type of C's parameter is the same as or a subclass of the type of A's parameter.

  • Since the parameter of the first catch block catches Exception and its subclasses, there's no chance for the second catch block to catch MyException.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic