aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception error" Watch "Exception error" New topic
Author

Exception error

M Rama
Ranch Hand

Joined: Mar 04, 2005
Posts: 91
When I do the following:



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




doesnot cause error. Why?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Also, have a look at Bug ID: 4046575 .
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Mar 01, 2005
Posts: 485
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

Joined: Mar 01, 2005
Posts: 485
any one please
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
anyone please
Rahul Bhosale
Ranch Hand

Joined: Mar 10, 2005
Posts: 77
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 ]

RB
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Exception error
     
    Similar Threads
    Exception
    Exceptions raised in catch block code
    compilation error in try/catch/finally
    Exception
    Is finally, really finally