aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exceptions & Overridding Example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exceptions & Overridding Example" Watch "Exceptions & Overridding Example" New topic
Author

Exceptions & Overridding Example

Esther Kak
Ranch Hand

Joined: Oct 11, 2006
Posts: 51
I am trying to relate the example from Chapter5 of K&B with one of the examwatches in Chapter2 (Page 104-Examwatch & Page Page390- Q3). But I have difficulty fixing the unreported exception error while compiling. Can anybody please explain.
Two topics involved here are
1. Overridding a method (I think I understood this right)
Overridding a method of the parent class which declares a checked exception with a method in the subclass that may or may not declare the exception. But if the overridden method declares an exception, it can be a run-time exception or an exception that is not broader than the parent class method.
2. Polymorphic super type reference referring to the subclass type (I still can't understand why the compiler cannot recognize this as the parent class method at compile time. Can somebody please explain this also)

Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
dude at compile time the compiler only knows about the reference variable and that is of Master type....and it can only assume that you are going to call the master method....it is only at the runtime that the jvm knows about the object and accordingly calls the appropriate method

but here at the compile time...you have declared that your master method throws an exception...since your method is throwing a checked exception....you have to put a try catch block around the line which calls the method...
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
this compiles because now the compiler doesnt have a problem since you are catching the error....or what you can do is declare that main method throws filenotfoundexception which is called ducking the exception
Esther Kak
Ranch Hand

Joined: Oct 11, 2006
Posts: 51
Thanks for your help
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
anytime...hope you understood
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

Ankur kothari wrote:this compiles because now the compiler doesnt have a problem since you are catching the error....or what you can do is declare that main method throws filenotfoundexception which is called ducking the exception


yes ankur is correct, it is because object create at run time not compile time...at the time of compilation, the compiler think this the method of Master class and it should be catch or throws the exception.


SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exceptions & Overridding Example