Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Compiler Errors with Exceptions  RSS feed

 
yogesh doshi
Ranch Hand
Posts: 49
Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below one gives me compile time error



Why below is vaild??

 
Henry Wong
author
Sheriff
Posts: 23026
120
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This, in my opinion, would really make a good test question ...

Granted, if you run the compiler on it, it takes 2 seconds to realize what is wrong. However, without the compiler, it is actually sneaky enough that it stumped me.

Henry
 
Narayana Bojja
Ranch Hand
Posts: 135
4
Hibernate Java Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below one gives me compile time error 


That code will give you compile time error. Have you remember checked exception rule ? . When you are calling a method if that method is throwing checked exception , you must declare or handle that exception where you are calling.

Here also same rule applies .



Here , you are creating object for Exception2 class. constructor is throwing FileNotFoundException. So you are declared  Exception in main method. Exception class can handle FileNotFoundException because it is  a super class of  FileNotFoundException. While creating object for Exception2, its  constructor will be called . Now from that constructor  there will be a call to super class constructor which throwing IOException. But in sub class constructor you are declared  FileNotFoundException which can't handle that   IOException. Because  IOException is a super class of FileNotFoundException.


Next coming to another code

Why below is vaild?? 


It works fine.


Here , you are creating object for Exception2 class. constructor is throwing FileNotFoundException,Exception. So you are declared  Exception in main method. Exception class can handle both Exception,  FileNotFoundException because it is  a super class of  FileNotFoundException. While creating object for Exception2, its  constructor will be called . Now from that constructor  there will be a call to super class constructor which throwing IOException.But in sub class constructor you are declared Exception which can  handle that   IOException. Because  Exception  is a super class of IOException. So this code compiles fine .


So the over all point is " super exception  class can handle both itself and its sub classes . sub classes can handle that particular corresponding  classes"

Hope this helps !

 
yogesh doshi
Ranch Hand
Posts: 49
Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks @Narayana
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!