Unreachable catch classes

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Wed Apr 27 10:23:09 UTC 2011


On 27/04/11 09:40, David Holmes wrote:
> Maurizio Cimadamore said the following on 04/27/11 18:35:
>> On 27/04/11 09:18, David Holmes wrote:
>>> What I suggested on coin-dev ( which has finally turned up now that 
>>> someone has unblocked the openjdk mail pipes ;-) ) is that instead 
>>> of doing an analysis of the expected thrown types, to simply warn 
>>> for the more common mistake of catching a superclass before a subclass.
>>
>> So, you are saying that the JDK 7 compiler should stop generating 
>> additional warnings, and start behaving as usual (we always had 
>> errors when catching supertype after subtype, and such).
>
> In that case I guess that is what I am suggesting.
>
> Is there a @SuppressWarnings setting for these warnings?
Currently those warnings cannot be suppressed - i.e. those are not Xlint 
warnings. However I see the merit of what you are proposing. When I 
changed javac to the current behavior there has been a lot of discussion 
about whether this should be a supressable Xlint warning, a mandatory 
warning or an error. We decided for the mandatory warning as we assumed 
this was bad code that could eventually be rejected in a future release 
- but we overlooked the Class.newInstance case, which makes the whole 
argument rather pointless.

Given that we should probably live with the Class.newInstance 'hole', I 
think that a suppressible warning would be the best option - i.e. user 
can decide whether the compiler should emit additional info about catch 
clause reachability (based on static information) by using a flag like 
-Xlint:try, and such warnings could then be suppressed the usual way 
(@SuppressWarnings("try")).

I will discuss this with my team and then get back to you.

Maurizio
>
> David
>
>> Maurizio
>>>
>>> David
>>>
>>>> Maurizio
>>>>>
>>>>> Cheers,
>>>>> David
>>>>>
>>>>> Joe Darcy said the following on 04/27/11 11:53:
>>>>>> Hello.
>>>>>>
>>>>>> FYI, javac in JDK 7 does a more precise analysis than before on 
>>>>>> what catch blocks can actually be reached and warnings are issued 
>>>>>> for unreachable catch blocks.  I've extracted those warnings from 
>>>>>> a recent full JDK build in case anyone wants to update the code 
>>>>>> in question.
>>>>>>
>>>>>> -Joe
>>>>>>
>>>>>> ../../../../../../src/share/classes/com/sun/corba/se/impl/protocol/BootstrapServerRequestDispatcher.java:124: 
>>>>>> warning: unreachable catch clause
>>>>>>        } catch (java.lang.Exception ex) {
>>>>>>          ^
>>>>>>  thrown type RuntimeException has already been caught
>>>>>>
>>>>>> ../../../../../../src/share/classes/com/sun/corba/se/impl/protocol/BootstrapServerRequestDispatcher.java:124: 
>>>>>> warning: unreachable catch clause
>>>>>>        } catch (java.lang.Exception ex) {
>>>>>>          ^
>>>>>>  thrown type RuntimeException has already been caught
>>>>>>
>>>>>> ../../../src/share/classes/java/util/concurrent/ThreadPoolExecutor.java:1115: 
>>>>>> warning: unreachable catch clause
>>>>>>                    } catch (Throwable x) {
>>>>>>                      ^
>>>>>>  thrown types RuntimeException,Error have already been caught
>>>>>> ../../../src/share/classes/java/net/DatagramSocket.java:183: 
>>>>>> warning: unreachable catch clause
>>>>>>        } catch(IOException e) {
>>>>>>          ^
>>>>>>  thrown type SocketException has already been caught
>>>>>> Note: Some input files use or override a deprecated API.
>>>>>> Note: Recompile with -Xlint:deprecation for details.
>>>>>> Note: Some input files use unchecked or unsafe operations.
>>>>>> Note: Recompile with -Xlint:unchecked for details.
>>>>>> 2 warnings
>>>>>>
>>>>>> ../../../src/share/classes/java/security/cert/X509CertSelector.java:2217: 
>>>>>> warning: unreachable catch clause
>>>>>>        } catch (CertificateException e3) {
>>>>>>          ^
>>>>>>  thrown types 
>>>>>> CertificateNotYetValidException,CertificateExpiredException have 
>>>>>> already been caught
>>>>>>
>>>>>> ../../../src/share/classes/javax/management/modelmbean/RequiredModelMBean.java:1220: 
>>>>>> warning: unreachable catch clause
>>>>>>        } catch (Exception e) {
>>>>>>          ^
>>>>>>  thrown types 
>>>>>> InvocationTargetException,IllegalAccessException,RuntimeException 
>>>>>> have already been caught
>>>>>> 1 warning
>>>>>>
>>>>>> ../../../../src/share/classes/sun/security/rsa/RSASignature.java:205: 
>>>>>> warning: unreachable catch clause
>>>>>>        } catch (GeneralSecurityException e) {
>>>>>>          ^
>>>>>>  thrown type BadPaddingException has already been caught
>>>>>>
>>>>>>
>>>>
>>




More information about the core-libs-dev mailing list