• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Rethrowing exception java SE7 K & B book

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

chapter 7, page number 396

as with multi-catch, you shouldnot be assigning a new value to catch parameter. The difference b/w this and multi-catch is where the compiler error occurs. For multi-catch, the compiler error will occur on the line where we attempt to assign a new value to the parameter, whereas here, the compilation error on the line where we throw e.


but I am not encountering such exception, my code is :

I am running this on JDK 8, is this the reason for my problem ??
 
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The throws clause is different in your code. The reason for the compilation failure in the book's code is because the method can't throw Exception but your method can...
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:
The throws clause is different in your code. The reason for the compilation failure in the book's code is because the method can't throw Exception but your method can...


but why is it so, as the method(in book) have thrown IOException and e is also assigned with the IOException, then why compilation error ?
 
Ankit Garg
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's simple, look at this:

When using a reference variable, compiler always looks at the declaration. So the compiler knows ex is of type Exception but the throws clause is incompatible with it...
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:
When using a reference variable, compiler always looks at the declaration. So the compiler knows ex is of type Exception but the throws clause is incompatible with it...


I was expecting the same reason, but got stuck at a point
look at this :

correct me if I am wrong in my //comment statement, and if not, then why not the compiler is not smart enough to find that exception is of type Exception and cannot be thrown by this method (as is finds in other cases) ..!!
 
Ankit Garg
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pushpender the compiler doesn't care what an object is initialized with after the initialization. It only considers the declared type of the variable. It's the same for all variables:

In the above code too the compiler doesn't know what obj is initialized with String so explicit cast is needed to assign it to a String variable. The code that you have in the previous post won't compile on older versions of Java. The new ability of the compiler to detect what type of exception is caught ends if you re-initialize the caught exception variable yourself...
 
The moustache of a titan! The ad of a flea:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic